MySQLdb not updating rows
pink at odahoda.de
Wed Jun 28 16:15:38 CEST 2006
> import md5
> import string
> import MySQLdb
> tc = raw_input("Teacher Code: ")
> p = raw_input("New Password: ")
> print tc
> hash = md5.new()
> print p
> print hash.hexdigest()
> h = hash.hexdigest()
> boo = raw_input("Sure you want to update password with above details? Y
> or N: ")
> if boo == 'y':
> db = MySQLdb.connect("copweb2", "******", "******", "*******")
> cursor = db.cursor()
> if cursor.execute("UPDATE teachers SET password = '%s' WHERE
> teacher_code = '%s'" % (h, tc)):
> print "Done"
> print "Error"
> print "cancelled"
> This code doesn't seem to update my database, anyone any idea why? Is
> it me being stupid? It doesn't kick out an error at all.
Another side note: don't build your queries using (dumb) string formatting,
let the MySQLdb module do it for you. More specifically use:
"UPDATE teachers SET password = %s WHERE teacher_code = %s",
"UPDATE teachers SET password = '%s' WHERE teacher_code = '%s'"
% (h, tc)
The former form takes care of quoting and escaping, your version did not
escape potentially harmful characters in tc, resulting in a possibly opened
door for SQL injection attacks.
Email: pink at odahoda dot de
More information about the Python-list