Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing list of parameters to SQL in psycopg2

I have a list of ids of rows to fetch from database. I'm using python and psycopg2, and my problem is how to effectively pass those ids to SQL? I mean that if I know the length of that list, it is pretty easy because I can always manually or automatically add as many "%s" expressions into query string as needed, but here I don't know how much of them I need. It is important that I need to select that rows using sql "id IN (id1, id2, ...)" statement. I know that it is possible to check the length of the list and concatenate suitable number of "%s" into query string, but I'm afraid that it would be very slow and ugly. Does anyone have an idea on how to solve it? And please don't ask why I need to do it with "IN" statement - it is a benchmark which is a part of my class assignment. Thanks in advance!

like image 661
k_wisniewski Avatar asked Dec 29 '11 18:12

k_wisniewski


People also ask

What is %s in SQL Python?

execute() arguments Passing parameters to a SQL statement happens in functions such as Cursor.execute() by using %s placeholders in the SQL statement, and passing a sequence of values as the second argument of the function. For example the Python function call: cur.

What does Psycopg do?

Psycopg converts Python variables to SQL values using their types: the Python type determines the function used to convert the object into a string representation suitable for PostgreSQL. Many standard Python types are already adapted to the correct SQL representation.


2 Answers

Python tuples are converted to sql lists in psycopg2:

cur.mogrify("SELECT * FROM table WHERE column IN %s;", ((1,2,3),)) 

would output

'SELECT * FROM table WHERE column IN (1,2,3);' 

For Python newcomers: It is unfortunately important to use a tuple, not a list here. Here's a second example:

cur.mogrify("SELECT * FROM table WHERE column IN %s;",      tuple([row[0] for row in rows])) 
like image 182
philofinfinitejest Avatar answered Oct 02 '22 21:10

philofinfinitejest


this question is old and maybe there is a newer one out there, but the answer my colleagues are going with right now is this:

sql = "SELECT * FROM table WHERE column = ANY(%(parameter_array)s)" cur.execute(sql,{"parameter_array": [1, 2, 3]}) 
like image 35
Brandon Henry Avatar answered Oct 02 '22 20:10

Brandon Henry