我有一个带有以下列表的 sqlite 数据库:
I have a sqlite database with a table with following columns :
id(int) , name(text) , dob(text)
我想插入以下字典:
{"id":"100","name":"xyz","dob":"12/12/12"}
字典键是列名.我怎样才能实现它?
Dictionary keys are the column names. How can i achieve it ?
这是一种保持参数安全的方法.(可能需要在表名部门打磨)
Here's a way which preserves parameter safety. (Might need polishing in the tablename department)
def post_row(conn, tablename, rec):
keys = ','.join(rec.keys())
question_marks = ','.join(list('?'*len(rec)))
values = tuple(rec.values())
conn.execute('INSERT INTO '+tablename+' ('+keys+') VALUES ('+question_marks+')', values)
row = {"id":"100","name":"xyz","dob":"12/12/12"}
post_row(my_db, 'my_table', row)
这篇关于Python:如何将字典插入到 sqlite 数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!