I have a Python script that queries a MySQL database every 5 seconds, gathering the latest three ID's for helpdesk tickets. I am using MySQLdb as my driver. But the issue is in my "while" loop, when I check if two arrays are equal. If they are NOT equal, I print "A new ticket has arrived." But this never prints! See my code:
import MySQLdb
import time
# Connect
db = MySQLdb.connect(host="MySQL.example.com", user="example", passwd="example", db="helpdesk_db", port=4040)
cursor = db.cursor()
IDarray = ([0,0,0])
IDarray_prev = ([0,0,0])
cursor.execute("SELECT id FROM Tickets ORDER BY id DESC limit 3;")
numrows = int(cursor.rowcount)
for x in range(0,numrows):
row = cursor.fetchone()
for num in row:
IDarray_prev[x] = int(num)
cursor.close()
db.commit()
while 1:
cursor = db.cursor()
cursor.execute("SELECT id FROM Tickets ORDER BY id DESC limit 3;")
numrows = int(cursor.rowcount)
for x in range(0,numrows):
row = cursor.fetchone()
for num in row:
IDarray[x] = int(num)
print IDarray_prev, " --> ", IDarray
if(IDarray != IDarray_prev):
print "A new ticket has arrived."
time.sleep(5)
IDarray_prev = IDarray
cursor.close()
db.commit()
Now when this ran, then I created a new ticket, the output looks like this:
[11474, 11473, 11472] --> [11474, 11473, 11472]
[11474, 11473, 11472] --> [11474, 11473, 11472]
[11474, 11473, 11472] --> [11474, 11473, 11472]
[11474, 11473, 11472] --> [11474, 11473, 11472]
[11475, 11474, 11473] --> [11475, 11474, 11473]
[11475, 11474, 11473] --> [11475, 11474, 11473]
[11475, 11474, 11473] --> [11475, 11474, 11473]
[11475, 11474, 11473] --> [11475, 11474, 11473]
[11475, 11474, 11473] --> [11475, 11474, 11473]
Where the format of my output is:
[Previous_Last_Ticket, Prev_2nd_to_last, Prev_3rd] --> [Current_Last, 2nd-to-last, 3rd]
Notice the change in numbers and, more importantly, the lack of "A new ticket has arrived"!