I'm trying to insert a list into a database with SQLite, the first value is an id with AUTOINCREMENT
property. This is the code:
# cursor.execute('''
# CREATE TABLE products (
# prod_id INTEGER PRIMARY KEY AUTOINCREMENT,
# prod_name VARCHAR(20),
# prod_brand VARCHAR(20),
# price FLOAT
# )
# ''')
products = [
('Keyboard', 'Doctor Key', 20.0),
('Mouse', 'MasterClick', 2.50)
]
cursor.executemany("INSERT INTO products VALUES (null,?,?,?), products")
I also tried doing this:
cursor.executemany("INSERT INTO products (prod_id,prod_name,prod_brand,price) VALUES (null,?,?,?),productos")
But in both cases I got the same error:
Traceback (most recent call last):
File "leccion2-1.py", line 38, in <module>
cursor.executemany("INSERT INTO products VALUES (null,?,?,?), products")
TypeError: function takes exactly 2 arguments (1 given)
This answer did not worked for me.
Thanks.
You need to supply the values outside of your quotes, since you are passing a variable, not a string.
cursor.executemany("INSERT INTO products VALUES (null,?,?,?)", products)
See also this example:
import sqlite3
con = sqlite3.connect("samples.db")
cur = con.cursor()
examples = [(2, "def"), (3, "ghi"), (4, "jkl")]
cur.executemany("INSERT INTO samples VALUES (?, ?)", examples)
for row in cur.execute("SELECT * FROM samples"):
print row