close

[Solved] psycopg2 Type: not all arguments converted during string formatting

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error psycopg2 Type: not all arguments converted during string formatting in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How psycopg2 Type: not all arguments converted during string formatting Error Occurs?

Today I get the following error psycopg2 Type: not all arguments converted during string formatting in python.

How To Solve psycopg2 Type: not all arguments converted during string formatting Error ?

  1. How To Solve psycopg2 Type: not all arguments converted during string formatting Error ?

    To Solve psycopg2 Type: not all arguments converted during string formatting Error It is recommended to not use string interpolation for passing variables in database queries

  2. psycopg2 Type: not all arguments converted during string formatting

    To Solve psycopg2 Type: not all arguments converted during string formatting Error It is recommended to not use string interpolation for passing variables in database queries

Solution 1

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

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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read