Search code examples
pythonsqldatabasemysql-connector

MySql: Inserting python lists into a table


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


Solution

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