[FIXED] Python excluding zeros from MySQL

Issue

Actually I'm doing an Alarm Clock Project using pi zero. So now the problem when the code fetch data from MySQL that is alarm time, it's goes to if condition to compare with current time. It's actually not working because python fetch not taking zero Infront of time. Example The zero Infront it been excluded... If (6:45:00 == 06:45:00) false

But it's doing great after 10:00:00.

mycursor.execute("SELECT * FROM alarm WHERE status = 1")
        
myresult = mycursor.fetchall()
       
for x in myresult:
    days = x[3].split(",")
    days = filter(None, days)

    for i in days:
        if(i == day):
            a = str(x[2])
            print(type(a))
            #alarm = a.strftime('%H:%M:%S')
            print("Today = " +str(x[0]))
            print(a)
            print(str(now))
            if(a==now):
                print("Alarm Rings")
                GPIO.setup(20, GPIO.OUT)
                GPIO.output(20, GPIO.LOW)
                GPIO.setup(21, GPIO.OUT)
                GPIO.output(21, GPIO.LOW)

Solution

Remove the leading zero from now before comparing:

if now[0] == '0':
    now = now[1:]
if a == now:
    # rest of the code


Answered By - Barmar
Answer Checked By - Senaida (sql.easybugfix Volunteer)

Related Posts

Post a Comment