python mysql update function from tuple issue, I need your help again

mysql, mysql-python, python

Solution

`cursor.execute()`expects a sql statement (as string) and an optional sequence of values, so it should have been either:

# this build a (statement, (values,....)) tuple
args = "UPDATE namen SET Name = '%s' WHERE `Name` = '%s'",(lookup[i],i)

# so you need positional arguments unpacking:
cursor.execute(*args) 

or

sql = "UPDATE namen SET Name = '%s' WHERE `Name` = '%s'"
cursor.execute(sql, (lookup[i],i))

For a sanitized and more readable version:

lookup={
    'Gigi':'Gigi Hofleitner',
    'Horst':'Horst Sergio'
}

# no need to create the same invariant string again and agin
sql="UPDATE namen SET Name=%s WHERE Name=%s"

for oldname, newname in lookup.items():
    cursor.execute(sql, (newname, oldname))

# better to commit only once    
connection.commit()

Problem

I would like to update several 10th of entries in my database. My idea is as follows, unfortunately, I get the following error. I tried to convert to a string, but it does not work. Any idea? TypeError: must be string or read-only buffer, not tuple ``` lookup={ 'Gigi':'Gigi Hofleitner', 'Horst':'Horst Sergio' } for i in lookup: sql="UPDATE namen SET Name = '%s' WHERE `Name` = '%s'",((lookup[i]),i) cursor.execute(sql) connection.commit() ```

Original source