Python MySQLdb循环

时间:2013-12-06 19:17:06

标签: python mysql

这是我想要实现的伪代码;

connect to db

While True:
execute query (SELECT * FROM requests WHERE state = 'NOT_STARTED'
foreach row in rows
  #process each row

这是我的代码;

import sys
import MySQLdb
import time

try:
    db = MySQLdb.connect(user='XXXX',
                         passwd='',
                         db='XXX',
                         host='XXX')

except MySQLdb.Error, e:
    raise e

while True:
    cursor = db.cursor()
    #db.begin()
    cursor.execute("SELECT * FROM requests WHERE state = 'NOT_STARTED'")
    res = cursor.fetchone()[0]
    #db.commit()
    #db.close()

    print "No records: %d" % res
    time.sleep(3)

但是,当我更新数据库时,记录数仍显示为1。

0 个答案:

没有答案