MySQLdb UPDATE does nothing

John Moore john at
Thu Sep 15 14:04:15 CEST 2005


I normally work with Java but I'm interested in using Python as well, 
particularly for little tasks like doing some massaging of data in a 
MySQL database. Below is my first attempt. I'm sure it's inelegantly 
written, but my main concern is that the UPDATE sql doesn't actually 
work, and I can't understand why. No error is returned, it's just that 
the update does not take place. The SQL itself is fine, though - if I 
instead write the SQL to a file I can use it from the mysql command line 
and it does all the updates just fine. What have I missed?




# import MySQL module
import MySQLdb

# connect
db = MySQLdb.connect(host="localhost", user="john", 

# create a cursor
cursor = db.cursor()

# execute SQL statement
cursor.execute("SELECT DISTINCT product_id FROM product_attribute")

# get the resultset as a tuple
result = cursor.fetchall()

# iterate through resultset
for record in result:
    sql="SELECT id FROM product_attribute WHERE product_id = 
    print " "+sql
    for record2 in result2:       
        sql="UPDATE product_attribute SET index_column = "+str(index)+" 
WHERE id = "+str(record2[0])
        print "   "+sql

More information about the Python-list mailing list