Whenever I try to use my addTask() and removeTask() functions I get a very long wait time followed by an error.
Here are the functions:
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.")
If I input a name that already exists in the addTask() function it works fine. However, if I try to add a new task with a new name in the addTask() function I get this error:
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')
Similarly for the remove function if I try with a non existential task name then it works fine. But if I try to remove a task that exists I get the same error.
The thing that really perplexes me is that it was working just fine a few minutes before and I hadn't even changed anything (or I can't recall what I changed).
Name
a primary key (or just add a unique constraint to it), you don't need theSELECT
in the first version; just do theINSERT
and the database will give you an error telling you the task already exists. – abarnert 14 hours agoSELECT
in the second one. Just do theDELETE
and the database will tell you whether it deleted 0 rows or 1. – abarnert 14 hours ago'
in it?), and prevents the database from optimizing your otherwise-equivalent statements, and generally makes Codd cry. Use parameterized statements:cur.execute("INSERT INTO Tasks(Name, Time) VALUES(%s, 0);", [name])
. – abarnert 14 hours ago