Search code examples
pythonsqlpostgresqlpsycopg2

How do you use ANY using psycopg2?


Using psycopg2, I want to do an update statement using a list of elements:

cursor.execute("UPDATE mytable SET mycol=2 WHERE name=ANY(%s) RETURNING id", 
                                    tuple(keywords))

where keywords is a list of strings, since name is a varchar column. However, I get:

TypeError: not all arguments converted during string formatting

How should I do the request?


Solution

  • Thanks @Adrian.

    It should be:

    cursor.execute("UPDATE mytable SET mycol=2 WHERE name=ANY(%s) RETURNING id", 
                                        tuple([keywords]))