How to use MySQL executemany to update list of lists in Python?
-
07-06-2021 - |
Question
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))
OTHER TIPS
update tablename set flag = 'yes'
WHERE (id,officeid) IN ((123,345), (23,45), (54,34));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow