1

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

2 Answers2

2
query = "INSERT INTO tableName (col1, col2) values (%s, %s)"
cursor.executemany(query, [(x, y) for x, y in zip(a, b)])
Booboo
  • 38,656
  • 3
  • 37
  • 60
0

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)])
Promaster
  • 162
  • 1
  • 12