ValueError usupported format character 'd' with psycopg2

后端 未结 2 1813

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\',          


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-18 15:31

    From the Psycopg FAQ:

    Q: I can’t pass an integer or a float parameter to my query: it says a number is required, but it is a number!

    A: In your query string, you always have to use %s placeholders, even when passing a number. All Python objects are converted by Psycopg in their SQL representation, so they get passed to the query as strings. See Passing parameters to SQL queries.

    Replace the %d with %s.

提交回复
热议问题