MySql: Inserting python lists into a table

烂漫一生 提交于 2021-02-10 17:48:49

问题


How do I insert these 2 lists into different SQL columns of the same table

a = [1,2,3]
b = [4,5,6]

This is the way to insert one list into a column

query = "INSERT INTO tableName (col1) VALUES (%s)"
cursor.executemany(query, [(r,) for r in a])

I can't seem to figure out how to insert both the lists into the table, I want list a to be in one column and list b to be in other column


回答1:


query = "INSERT INTO tableName (col1, col2) values (%s, %s)"
cursor.executemany(query, [(x, y) for x, y in zip(a, b)])



回答2:


I would do something like this:

query = "INSERT INTO sometable (somecol, somecol2) values (something, something)"
cursor.executemany(query, [(v, x) for v, x in zip(t, h)])


来源:https://stackoverflow.com/questions/63962866/mysql-inserting-python-lists-into-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!