问题
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