Python:如何将字典插入到sqlite数据库? [英] Python : How to insert a dictionary to a sqlite database?
本文介绍了Python:如何将字典插入到sqlite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
id(int),name(text),dob (文字)
我想插入以下字典:
{id:100,name:xyz,dob:12/12/12}
字典键是列名。如何实现?
解决方案
查看文档这里,您可以添加一行:
c。执行(INSERT INTO股票价值(?,?,?),[dict [id],dict [name],dict [dob]]
或者您可以使用列表并一次添加多行:
#一次插入许多记录的更大的例子
purchases = [('2006-03-28','BUY','IBM',1000,45.00),
'2006-04-05','买入','MSFT',1000,72.00),
('2006-04-06','SELL','IBM',500,53.00),
)
c.executemany('INSERT INTO stocks VALUES(?,?,?,?,?)',购买)
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)
这篇关于Python:如何将字典插入到sqlite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文