postgresqlpg8000

pg8000.core.ProgrammingError: 'could not determine data type of parameter $2'


I'm using pg.8000 (Postgres) and trying to run the following SELECT query

cursor.execute(
        """
        SELECT orders.name, orders.order_price, orders.selling_price, orders.earnings
        FROM member, orders
        WHERE member.id=orders.member_id
        AND member.name = %s
        """,
        member_username
    )

Where member.username is a String.

But I am getting the following error.

pg8000.core.ProgrammingError: ('ERROR', 'ERROR', '42P18', 'could not determine data type of parameter $2', 'postgres.c', '1350', 'exec_parse_message', '', '')

However, when I run same query using GUI tool, everything runs fine and I get the results. What is the problem?


Solution

  • You passed the parameter wrong, you should give a tuple, a list or a dictionary

    Example with a tuple:

    cursor.execute(
            """
            SELECT orders.name, orders.order_price, orders.selling_price, orders.earnings
            FROM member, orders
            WHERE member.id=orders.member_id
            AND member.name = %s
            """,
            (member_username,)
        )
    

    Example with a list:

    cursor.execute(
            """
            SELECT orders.name, orders.order_price, orders.selling_price, orders.earnings
            FROM member, orders
            WHERE member.id=orders.member_id
            AND member.name = %s
            """,
            [member_username]
        )
    

    Example with a dictionary:

    cursor.execute(
            """
            SELECT orders.name, orders.order_price, orders.selling_price, orders.earnings
            FROM member, orders
            WHERE member.id=orders.member_id
            AND member.name = %(mname)s
            """,
            {'mname' : member_username}
        )
    

    http://initd.org/psycopg/docs/usage.html#query-parameters