1

This is my database

database

i want to comparing 2 date time datatype (datetime_comp & date_time) simple comparison. etc : date1 > date 2 = "On_Time"

but my problem is, i need to compare the data 1 by 1 (loop for sure) and this is my code

from datetime import datetime
import pymysql
import time

#Declare Connection
conn = pymysql.connect(host='localhost', port='', user='root', passwd='', db='tes_coba', use_unicode=True, charset="utf8mb4")
cur = conn.cursor()

EW = "Tepat Waktu"
LW = "Tidak Tepat Waktu"

#get data from database
cur.execute("SELECT datetime_comp FROM `tweet2`")
row1 = cur.fetchall()
cur.execute("SELECT date_time FROM `tweet2`")
row2 = cur.fetchall()

n  = 1

for date1 in row1:
    print(date1)

for date2 in row2:
    print(date2)

if date1 > date2:
    Result=EW
elif date1 < date2:
    Result=LW

print(Result)
cur.execute("UPDATE tweet2 SET on_time=%s WHERE no=%s AND relevance='Relevan'",(str(Result), str(n)))
n = n + 1


conn.commit()
cur.close()
conn.close()

and the result is, the comparison code only work with last data in database, because the comparison code does not looping for sure (confusing)

but if i put the comparison code to one if the looping process,

for date1 in row1:
    print(date1)

for date2 in row2:
    print(date2)

    if date1 > date2:
        Result=EW
    elif date1 < date2:
        Result=LW

only the last data compare with all data

6
  • Because you are comparing it out of the loop! Commented Jul 25, 2018 at 4:47
  • i knew it for sure, but if i comparing inside the loop, only the last database comparing with the loop data (if u know what i mean) Commented Jul 25, 2018 at 4:57
  • @experiment forgot to mention. if u have any solution sir ? Commented Jul 25, 2018 at 5:04
  • you will get NameError if date1 == date2 Commented Jul 25, 2018 at 5:05
  • also it will be better to SELECT both columns in one query like SELECT datetime_comp, date_time FROM tweet2 and iterate over tuples Commented Jul 25, 2018 at 5:08

2 Answers 2

2

First of all, you do not need two separate queries to match two columns of the same table. It can be done in the same query.

cur.execute("SELECT * FROM `tweet2`")
records = cur.fetchall()

iterate the records:

for record in records:
    if record[2] < record[5]: # I'm assuming the column index 2 and 5 have the relevant table column values
        do something here
    else:
        do something else here
Sign up to request clarification or add additional context in comments.

1 Comment

really thanks to you man, this is work. i didnt know how to mention the record before. thanks again :D
1

Firstly load your table into a dataframe and np.where to compare columns to form a new column

import pymysql
import time
import numpy as np
import pandas as pd

#Declare Connection
conn = pymysql.connect(host='localhost', port='', user='root', passwd='', db='tes_coba', use_unicode=True, charset="utf8mb4")


query = "SELECT date_time,datetime_comp FROM tweet2"
df = pd.read_sql(query, conn)

df['result'] = np.where(df['datetime_comp']>df["date_time"], 'Tepat Waktu', 'Tidak Tepat Waktu')

I hope it works for you!

6 Comments

(ValueError: DataFrame constructor not properly called!) error sir @experiment. could you help with pymysql library ?
@Nanda Dont call me sir/maam! What kind of data you get when you run the query SELECT date_time,datetime_comp FROM tweet2 ?
okay sorry man :D, its going wrong when df = pd.DataFrame(cur.fetchall()) and pop the error
Why do we need pandas and np for such task?
@AnkitJaiswal There are other solutions as well. I prefer loading the data to dataframes to do the processing part.As nanda wanted an extra column of results ,using dataframes we can get that effectively!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.