I have a sqlite database with a table with following columns :
id(int) , name(text) , dob(text)
I want to insert following dictionary to it :
{"id":"100","name":"xyz","dob":"12/12/12"}
Dictionary keys are the column names. How can i achieve it ?
解决方案
Looking at the documentation here you can add a single row:
c.execute("INSERT INTO stocks VALUES (?,?,?)", [dict["id"], dict["name"], dict["dob"]])
Or you can use a list and add multiple rows in one go:
# Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)