I’m running into some trouble running python/mysqldb on my raspberry pi. This is a pretty simple script, so I’m not sure what I’m missing. The “SELECT * FROM…” runs with no problem, but I can’t seem to update the table with new values. The script runs without throwing errors, but when I ctrl-C, it gives me this:
Exception _mysql_exceptions.ProgrammingError: (2014, “Commands out of sync; you can’t run this command now”) in bound method DictCursor.__del of MySQLdb.cursors.DictCursor object at 0x19dfd90
Here’s my script:
dhost = "localhost"
duser = "root"
dname = "rpi"
dpass = "datPassword"
import MySQLdb
try:
con = MySQLdb.connect(dhost,duser,dpass,dname);
cur = con.cursor(MySQLdb.cursors.DictCursor)
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
def websiteToSensor():
cur.execute("SELECT * FROM homeauto WHERE changedby = 'website'")
rows = cur.fetchall()
for row in rows:
cur.execute("UPDATE homeauto SET changedby = 'script' WHERE id = '%s'",(row["id"]))
return
while True:
websiteToSensor()
Does anyone have an idea as to why my table isn’t updating? Thanks!
***EDIT: SOLUTION***
Thanks to Martijn Pieters, here’s my new websiteToSensor() code:
def websiteToSensor():
cur = con.cursor(MySQLdb.cursors.DictCursor)
cur.execute("SELECT * FROM homeauto WHERE changedby = 'website'")
rows = cur.fetchall()
num = int(cur.rowcount)
if num > 0:
for row in rows:
cur.execute("UPDATE homeauto SET changedby = 'script' WHERE id = '%s'",(row["id"]))
con.commit()
cur.close()
con.commit()
else:
cur.close()
con.commit()
return
Try committing your changes: