Skip to content

Instantly share code, notes, and snippets.

@leveled
Created December 20, 2021 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leveled/5027e976e9d3026a38dc812febf4f85f to your computer and use it in GitHub Desktop.
Save leveled/5027e976e9d3026a38dc812febf4f85f to your computer and use it in GitHub Desktop.
Using python to insert a dictionary into an SQL table
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