0

I am facing problem in writing PostgreSQL select statement in Python when I have to pass more than one values in a statement. Below is the sample example.

 from sqlalchemy import create_engine

 values = [ 'a' , 'b' ]

 con = create_engine(conn_string)

 results = con.execute( "select * from table where var in (%s)", [ values ] )

I am able to run query with single value but getting format error for an array.

halfer
  • 19,824
  • 17
  • 99
  • 186
Shreyak
  • 326
  • 3
  • 10
  • In the case of the duplicate, you need to change `?` to `%s` since postgres uses different placeholders, but the principle is identical. – roganjosh Oct 18 '17 at 15:01
  • If you're using psycopg2 as your DB-API driver, you could pass it a tuple as the argument and it'll form a suitable form for the IN operator: http://initd.org/psycopg/docs/usage.html#tuples-adaptation. No need to manually create comma separated placeholders. – Ilja Everilä Oct 18 '17 at 17:11

1 Answers1

0

I found the answer in stackoverflow . It is a duplicate question. i was overthinking because of postgresql.

Executing “SELECT … WHERE … IN …” using MySQLdb

Shreyak
  • 326
  • 3
  • 10