This is my 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

NameErrorifdate1==date2SELECTboth columns in one query likeSELECT datetime_comp, date_time FROM tweet2and iterate over tuples