Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mals14/89e5f6ab04d8f952093d86714244c929 to your computer and use it in GitHub Desktop.
Save mals14/89e5f6ab04d8f952093d86714244c929 to your computer and use it in GitHub Desktop.
example of how to insert a dictionary into sqlite database python
# example of how to insert a dictionary into sqlite database
# suorce - https://stackoverflow.com/questions/10913080/python-how-to-insert-a-dictionary-to-a-sqlite-database
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment