Created
December 20, 2021 18:28
-
-
Save leveled/5027e976e9d3026a38dc812febf4f85f to your computer and use it in GitHub Desktop.
Using python to insert a dictionary into an SQL table
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
placeholders = ', '.join(['%s'] * len(myDict)) | |
columns = ', '.join(myDict.keys()) | |
sql = "INSERT INTO %s ( %s ) VALUES ( %s )" % (table, columns, placeholders) | |
# valid in Python 2 | |
cursor.execute(sql, myDict.values()) | |
# valid in Python 3 | |
cursor.execute(sql, list(myDict.values())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment