Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError usupported format character 'd' with psycopg2

I have code like this:

print "company_id = %d" % company_id
...
db.cursor.execute("insert into person (company_id, first, last, type) values (%d, '%s', '%s', %d);", (company_id, name[0], name[1], type))

I get the following output:

company_id = 1
Traceback (most recent call last):
...
  File "./GetPeople.py", line 125, in insertPerson
    db.cursor.execute("insert into person (company_id, first, last, type) values (%d, '%s', '%s', %d);",     

Why can it print out the first line but it gives an error for the db.cursor.execute() call?

like image 733
Stephen Rasku Avatar asked Jul 01 '13 17:07

Stephen Rasku


1 Answers

The single quotes around the %s placeholders are incorrect and the %d isn't used as per the docs. Change

db.cursor.execute("insert into person (company_id, first, last, type) values (%d, '%s', '%s', %d);", (company_id, name[0], name[1], type))

to

db.cursor.execute("insert into person (company_id, first, last, type) values (%s, %s, %s, %s);", (company_id, name[0], name[1], type))
like image 122
Eric Workman Avatar answered Sep 27 '22 21:09

Eric Workman