MySQLDb上的名称错误

时间:2014-12-22 23:33:05

标签: python

我正在您的帐户系统上处理此Python Credits,但似乎我的最后一个for循环未运行导致time_set未定义。有人知道为什么我的最后一个for循环没有运行。出于安全原因,已删除数据库详细信息。这是我的代码:

from time import sleep
import MySQLdb
db = MySQLdb.connect(host="", # your host, usually localhost
                     user="", # your username
                      passwd="", # your password
                      db="") # name of the data base
cursor = db.cursor()#Defines a cursor allowing me to execute SQL commands
def timer(time, _print=False):
    if time == 0:
        cursor.execute("SELECT * FROM Accounts WHERE Username='{0}';".format(name))
        cursor.execute("UPDATE Accounts SET Credits = 0 WHERE Username = '{1}';".format(creds, name))
        return ''
    else:
        if _print: print time
        sleep(1)
        return timer(time - 1, _print)
name = raw_input("What username would you like to add credits to?:")
cursor.execute("SELECT * FROM Accounts WHERE Username='{0}';".format(name))
creds = input("How many credits would you like to add to the account?")
for test in cursor.fetchall():
    creds = creds + test[3]
cursor.execute("UPDATE Accounts SET Credits = {0} WHERE Username = '{1}';".format(creds, name))
for row in cursor.fetchall():
    time_set = creds * 60
    print time_set
timer(time_set, True)

2 个答案:

答案 0 :(得分:1)

您似乎正在尝试从UPDATE查询中提取行,这将使您无处可去,因为UPDATE不会返回任何行。

答案 1 :(得分:0)

我似乎已经弄明白了这个问题。我意识到第二个for循环是完全没必要的,因为我只是把积分计算成几分钟。所以我删除了

for row in cursor.fetchall():
    time_set = creds * 60
    print time_set

并将其替换为

time_set = creds * 60