Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues using python's 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})

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

It seems to be concerning the , but I have checked and all the parenthesis are properly nested (none enclosing an errant ,)

like image 804
chustar Avatar asked Oct 18 '11 04:10

chustar


2 Answers

You need an "s" after each of those positional arguments.

(%(cname)s, %(symbol)s,  ....
like image 144
imm Avatar answered Oct 01 '22 06:10

imm


What @imm said. Also, you may want to 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))
like image 39
sberry Avatar answered Oct 01 '22 06:10

sberry