5
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 ,)

1
  • This is not how you're supposed to do it. Commented Oct 18, 2011 at 4:06

2 Answers 2

17

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

(%(cname)s, %(symbol)s,  ....
Sign up to request clarification or add additional context in comments.

1 Comment

You saved me hours of debugging. God bless you, sir. This also applies to Postgres using psycopg2 :)
3

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))

1 Comment

This response is valid but also you may want to map columns as @imm sais in his response, using dicts. Very confortable way to do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.