Problems using python string formatting libraries

cur.execute('INSERT INTO company VALUES (%(cname), %(symbol), %(start_date), %(end_date))' %{'cname' : company, 'symbol' : company, 'start_date' : startdate, 'end_date' : enddate}) 

Attempting to run this string on my computer results in a string formatting error: ValueError: unsupported format character ',' (0x2c) at index 36

It seems as for , but I checked and all the brackets are correctly nested (none of them contain an erroneous one , )

+4
source share
2 answers

What @imm said. Alternatively, you can use the built-in query formatting that is part of MySQLdb.

 cur.execute("INSERT INTO company VALUES (%s, %s, %s, %s)", (company, company, startdate, enddate)) 
+2
source

After each of these positional arguments, you need an "s".

 (%(cname)s, %(symbol)s, .... 
+15
source

All Articles