How to use python dict as data source for mysql insert-into statement.

placeholders = ', '.join(['%s'] * len(dict_data))
columns = ', '.join(dict_data.keys())
sql = "INSERT INTO %s ( %s ) VALUES ( %s )" % (table, columns, placeholders)
cursor.execute(sql, dict_data.values())

See also this stackoverflow for reference

BACK TO TOP
Post tagged with: mysql, python, database