简体   繁体   中英

Constructing an SQL query with a list of variable length with sqlalchemy and pandas in python

I would like to construct a SQL statement with an IN operator that works on a list of arbitrary length. I am working with python, pandas, and sqlalchemy.

For example, If the query I'd like to execute is

SELECT * FROM users WHERE age IN (25, 26, 27)"

I have tried:

import pandas as pd
from sqlalchemy.sql import text

ages = (25, 26, 27)
query = text("SELECT * FROM users WHERE age IN (:x)")
df = pd.read_sql_query(query, conn, params={"x":ages})

But this results in errors. How can I construct the query properly? I would like the solution to work in the case that there is only one value in the list for the IN operator, ie ages = tuple(25) .

One solution is to use python's string join() method.

The example would read:

import pandas as pd
from sqlalchemy.sql import text

ages = (25, 26, 27)
ages = (str(n) for n in ages)
ages = ','.join(ages)
query = text("SELECT * FROM users WHERE age IN (:x)")
df = pd.read_sql_query(query, conn, params={"x":ages})

Consider a dynamic SQL string build for the placeholders and a dynamic dictionary build using dictionary comprehension for the parameters. Below assumes your RDMS is SQLite with the colon named parameters:

import pandas as pd
from sqlalchemy.sql import text

ages = (25, 26, 27)
placeholders = ', '.join([':param'+str(i) for i in range(len(ages))])
paramdict = {'param'+str(i): ages[i] for i in range(len(ages))}

query = text("SELECT * FROM users WHERE age IN ({})".format(placeholders))
# SELECT * FROM users WHERE age IN (:param0, :param1, :param2)

df = pd.read_sql_query(query, conn, params=paramdict)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM