Created
June 14, 2020 16:15
-
-
Save mals14/89e5f6ab04d8f952093d86714244c929 to your computer and use it in GitHub Desktop.
example of how to insert a dictionary into sqlite database python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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