psycopg2 TypeError: not all arguments converted during string formatting

Question:

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.

Asked By: konart

||

Answers:

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:
Passing table name as a parameter in psycopg2
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'],))
Answered By: ndpu

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.

Answered By: RjOllos

Use AsIs

from psycopg2.extensions import AsIs

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

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.

Answered By: rsacc

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,))
Answered By: Spencer Sutton

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.

Answered By: user16843208

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

Answered By: Jodmoreira
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.