Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
Python psycog2 | Use a tuple or array in where clause

I have a tuple like as shown below

vilist = (1,2,3,4)

I am trying to use them in Psycopg2 query like as shown below

sql = "select * from temp.table1 where ids in {}"
cur.execute(sql,vilist)

Which should parse a SQL string like

SELECT * FROM temp.table1 WHERE ids IDS (1,2,3,4);

However, I get an error like as shown below

SyntaxError                               Traceback (most recent call last)
<ipython-input-91-64f840fa2abe> in <module>
      1 sql = "select * from temp.table1 where ids in {}"
----> 2 cur.execute(sql,vilist)

SyntaxError: syntax error at or near "{"
LINE 1: ...rom temp.table1 where ids in {}

Can help me resolve this error please.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use SQL string composition to pass identifiers or literals to a query text.

import psycopg2
import psycopg2.sql as sql
# ...

vilist = (1,2,3,4)
query = sql.SQL("select * from temp.table1 where ids in {}").format(sql.Literal(vilist))
cur.execute(query)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!