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.
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.
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.
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.
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.
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,))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With