I’m having trouble finding any information on how to do error checking on MySQLdb. I have been trying to do a simple update command for a MySQL database and it simply is not working. No matter how I change the terms, or the type of variables I submit to it.
Here are some of my (commented out) attempts:
timeid = twitseek['max_id']
#timeup = "UPDATE `timeid` set `timestamp`='" + str(timeid) + "';"
#print timeup
#c.execute(timeup)
#timeup = "UPDATE timeid SET timestamp=\"" + str(timeid) + "\"";
#timeup = "UPDATE timeid set timestamp = '500';"
timeup = 500
c.execute("""UPDATE timeid SET timestamp = %d;""", timeup)
#c.execute(timeup)
All I want to do is upload the value of timeid to the timestamp column’s first value (or any value) in the table timeid.
Nothing I do seems to work and I’ve been sitting here for literally hours trying countless iterations.
You seem to be missing an obligatory call to
.commit()on your connection object to commit your change.The above method will produce valid SQL, but you don’t get the security benefit of prepared statements this way. The proper method to pass in parameters is to use
%s, and pass in a tuple of parameters:From the MySQLdb FAQ:
As far as error checking goes, a failed connection or a syntactically invalid query will throw an exception. So you would want to wrap it in a
try/exceptas is common in Python.