I am trying to make dynamic insert command into mysql, from dictionary values in one transaction but getting an error. Also I was wondering what is the most efficient way to perform this specific case (maybe my code is not optimal)? I am using FOR since in some cases dictionary can be empty. Thanks
import mysql.connector
mydb = mysql.connector.connect(..........
mycursor = mydb.cursor()
varStatic="test"
cust={'74.2': '54', '172.26': '76', '7': 'B9'}
insertStatement='"""INSERT INTO customers (id,number,desc) VALUES (%s,%s,%s)"""'
for key in cust:
insertStatement+=',('+key+','+cust[key]+','+varStatic+')'
mycursor.execute(insertStatement)
mydb.commit()