【发布时间】:2012-04-01 20:25:02
【问题描述】:
我有一个 Python 脚本,它每 5 秒查询一次 MySQL 数据库,收集最新的三个 ID 以获得帮助台票证。我使用 MySQLdb 作为我的驱动程序。但问题出在我的“while”循环中,当我检查两个数组是否相等时。如果它们不相等,我会打印“新票已到”。但这永远不会打印!查看我的代码:
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()
现在运行时,我创建了一个新票,输出如下所示:
[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]
我的输出格式是:
[Previous_Last_Ticket, Prev_2nd_to_last, Prev_3rd] --> [Current_Last, 2nd-to-last, 3rd]
请注意数字的变化,更重要的是,缺少“新票已到”!
【问题讨论】:
标签: python while-loop logic