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.

2
  • In the case of the duplicate, you need to change ? to %s since postgres uses different placeholders, but the principle is identical. Commented Oct 18, 2017 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: initd.org/psycopg/docs/usage.html#tuples-adaptation. No need to manually create comma separated placeholders. Commented Oct 18, 2017 at 17:11

1 Answer 1

0

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

Executing “SELECT … WHERE … IN …” using MySQLdb

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.