Search code examples
pythonmysqllistexecutemany

How to use MySQL executemany to update list of lists in Python?


I have a list of lists : myList = [[123, 345], [23, 45], [54, 34]]

I want to do something like this :

cursor.executemany("update tablename set flag = 'yes' where id = %s and officeid = %s", myList)

I searched for this and found no solution. If it will never be implemented, what is a better update statement without having to loop through all the elements in the list?


Solution

  • myTuples = tuple(map(tuple, myList))
    
    cursor.execute("update tablename set flag = 'yes' \
    where (id, officeid) in " + str(myTuples))