Skip to content
Advertisement

Insert into with Cursor.executemany and a Python list

Can someone please explain to me what is wrong with this code? I get the “parameters are of unsupported type” error. Is it not possible to feed a list into cursor.executemany? The type of the column is float not null.

random_numbers = [
    1,
    2,
    3,
]

cursor.executemany('INSERT INTO Table (Column) VALUES (?)', random_numbers)

Advertisement

Answer

The values for cursor.executemany must be a sequence of tuples:

cursor.executemany('INSERT INTO Table (Column) VALUES (?)', 
                   [(n,) for n in random_numbers])

This is a logical extension of cursor.execute requiring a single tuple for the values argument.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement