psycopg2 TypeError: not all arguments converted during string formatting

PythonTracPsycopg2

Python Problem Overview


I'm trying execute a simple query, but getting this error no matter how I pass the parameters.

Here is the query (I'm using Trac db object to connect to a DB):

cursor.execute("""SELECT name FROM "%s".customer WHERE firm_id='%s'""" % (schema, each['id']))

schema and each['id'] both are simple strings

print("""SELECT name FROM "%s".customer WHERE firm_id='%s'""" % (schema, each['id']))

Result: SELECT name FROM "Planing".customer WHERE firm_id='135'

There is on error is a remove quote after firm_id=, but that way parameter is treated a an integer and ::text leads to the very same error.

Python Solutions


Solution 1 - Python

In my case I didn't realize that you had to pass a tuple to cursor.execute. I had this:

cursor.execute(query, (id))

But I needed to pass a tuple instead

cursor.execute(query, (id,))

Solution 2 - Python

I got this same error and couldn't for the life of me work out how to fix, in the end it was my mistake because I didn't have enough parameters matching the number of elements in the tuple:

con.execute("INSERT INTO table VALUES (%s,%s,%s,%s,%s)",(1,2,3,4,5,6))

Note that I have 5 elements in the values to be inserted into the table, but 6 in the tuple.

Solution 3 - Python

It is recommended to not use string interpolation for passing variables in database queries, but using string interpolation to set the table name is fine as long as it's not an external input or you restrict the allowed value. Try:

cursor.execute("""
    SELECT name FROM %s.customer WHERE firm_id=%%s
    """ % schema, (each['id'],))

Rules for DB API usage provides guidance for programming against the database.

Solution 4 - Python

Use AsIs

from psycopg2.extensions import AsIs

cursor.execute("""
    select name 
    from %s.customer 
    where firm_id = %s
    """, 
    (AsIs(schema), each['id'])
)

Solution 5 - Python

You could try this:

cursor.execute("INSERT INTO table_name (key) VALUES(%s)",(value1,))

You will get an error if you are missing a (,) after value1.

Solution 6 - Python

The correct way to pass variables in a SQL command is using the second argument of the execute() method. And i think you should remove single quotes from second parameter, read about it here - http://initd.org/psycopg/docs/usage.html#the-problem-with-the-query-parameters.

Note that you cant pass table name as parameter to execute and it considered as bad practice but there is some workarounds:
https://stackoverflow.com/questions/13793399/passing-table-name-as-a-parameter-in-psycopg2
https://stackoverflow.com/questions/9354392/psycopg2-cursor-execute-with-sql-query-parameter-causes-syntax-error

To pass table name try this:

cursor.execute("""SELECT name FROM "%s".customer WHERE firm_id=%s""" % (schema, '%s'), (each['id'],))

Solution 7 - Python

Every time I have this kind of error, I am passing the wrong amount of values. Try check it

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkonartView Question on Stackoverflow
Solution 1 - PythonSpencer SuttonView Answer on Stackoverflow
Solution 2 - PythonrsaccView Answer on Stackoverflow
Solution 3 - PythonRjOllosView Answer on Stackoverflow
Solution 4 - PythonClodoaldo NetoView Answer on Stackoverflow
Solution 5 - Pythonuser16843208View Answer on Stackoverflow
Solution 6 - PythonndpuView Answer on Stackoverflow
Solution 7 - PythonJodmoreiraView Answer on Stackoverflow