每当我尝试使用addTask()和removeTask()函数时,我会得到很长的等待时间,然后出现错误。
以下是功能:
def addTask():
name = raw_input("Enter the name of the task you would like to add: ")
exists = cur.execute("SELECT Name FROM Tasks WHERE Name = '%s';" % name)
if exists:
print("Task already exists.")
else:
cur.execute("INSERT INTO Tasks(Name, Time) VALUES('%s', 0);" % name)
print("'%s' has been added to the task list." % name)
def removeTask():
name = raw_input("Enter name of the task you would like to delete: ")
exists = cur.execute("SELECT Name FROM Tasks WHERE Name = '%s';" % name)
if exists:
cur.execute("DELETE FROM Tasks WHERE Name = '%s';" % name)
print("'%s' has been removed from the task list." % name)
else:
print("Task doesn't exist.")
如果我输入addTask()函数中已存在的名称,它可以正常工作。但是,如果我尝试在addTask()函数中添加一个具有新名称的新任务,我会收到此错误:
Traceback (most recent call last):
File "./timelog3.py", line 137, in <module>
main()
File "./timelog3.py", line 23, in main
addTask()
File "./timelog3.py", line 44, in addTask
cur.execute("INSERT INTO Tasks(Name, Time) VALUES('%s', 0);" % name)
File "/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.4b4-py2.7-linux- x86_64.egg/MySQLdb/cursors.py", line 202, in execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.4b4-py2.7-linux-x86_64.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1205, 'Lock wait timeout exceeded; try restarting transaction')
类似于删除功能,如果我尝试使用非存在性任务名称,那么它可以正常工作。但是,如果我尝试删除存在的任务,我会得到同样的错误。
让我感到困惑的是,它在几分钟前工作得很好,我甚至没有改变任何东西(或者我不记得我改变了什么)。
答案 0 :(得分:0)
你得到的错误是说有人锁定了表(或整个数据库,或数据的其他一些子集)。
所以,或者另一个程序正在使用数据库,你的程序有多个数据库连接,或者你的程序只有一个连接,并试图在它上面做两件事交错(或者甚至在同一时间,如果你有多线程)。
一种常见的可能性是您启动了脚本,将其放在后台,然后再次启动相同的脚本,现在他们正在互相争斗。几分钟前,你没有运行两个副本,所以一切正常。