Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

psycopg2 TypeError: not all arguments converted during string formatting

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.

like image 548
konart Avatar asked Feb 03 '14 10:02

konart


People also ask

What is TypeError not all arguments converted during string formatting in Python?

TypeError: not all arguments converted during string formatting is an error in Python that often occurs in the following cases: When the number of format specifiers is less than the number of values passed. Let’s see an example of each case along with the solution to prevent the errors.

Is there a shortcut for% string in psycopg?

There is no shortcut as in the % string operator. Show activity on this post. I try to execute a query but it fails with the error not all arguments converted during string formatting (or object does not support indexing). Why? Psycopg always require positional arguments to be passed as a sequence, even when the query takes a single parameter.

What does type error not converted during string formatting mean?

The Problem: typeerror: not all arguments converted during string formatting A TypeError is a type of error that tells us we are performing a task that cannot be executed on a value of a certain type. In this case, our type error relates to a string value. Python offers a number of ways that you can format strings.

What is a TypeError in Python?

A TypeError is a type of error that tells us we are performing a task that cannot be executed on a value of a certain type. In this case, our type error relates to a string value. Python offers a number of ways that you can format strings. This allows you to insert values into strings or concatenate values to the end of a string.


2 Answers

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,)) 
like image 168
Spencer Sutton Avatar answered Sep 19 '22 13:09

Spencer Sutton


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.

like image 31
rsacc Avatar answered Sep 21 '22 13:09

rsacc