Where子句中的Python ibm_db列表
我正在使用Python读取csv文件中的名称,然后将这些名称合并到sql查询中。使用ibm_sql,我试图将列表变量放入SQLWHERE子句中:WHERE in list 如何在WHERE子句中引用列表示例?:WHERE a.name in sampleWhere子句中的Python ibm_db列表,python,sql,python-3.x,jupyter-notebook,db2,Python,Sql,Python 3.x,Jupyter Notebook,Db2,我正在使用Python读取csv文件中的名称,然后将这些名称合并到sql查询中。使用ibm_sql,我试图将列表变量放入SQLWHERE子句中:WHERE in list 如何在WHERE子句中引用列表示例?:WHERE a.name in sample 考虑插入一个逗号分隔的qmark列表?等于列表中的项目数。然后,在执行调用中将列表作为参数传递: 考虑插入一个逗号分隔的qmark列表?等于列表中的项目数。然后,在执行调用中将列表作为参数传递: #pip install ibm-db impo
考虑插入一个逗号分隔的qmark列表?等于列表中的项目数。然后,在执行调用中将列表作为参数传递:
考虑插入一个逗号分隔的qmark列表?等于列表中的项目数。然后,在执行调用中将列表作为参数传递:
#pip install ibm-db
import ibm_db_dbi as db
conn = db.connect("DATABASE=xxx;HOSTNAME=xxx;PORT=xxx;PROTOCOL=TCPIP;UID=xxx;PWD=xxx;", "", "")
with open('file.csv', newline='') as f:
reader = csv.reader(f)
next(reader, None) #skip header
data = list(reader) #list
sample = data[:20]
#sql
cursor = conn.cursor()
sql = '''
SELECT name
FROM (
SELECT DISTINCT a.name, b.number
FROM table1 as a
JOIN table2 as b ON a.sk = b.sk
WHERE a.name IN _____________ <<<
order by b.number
)
group by name
'''
#{}.format(sample)
cursor.execute(sql)
for r in cursor.fetchall():
print(r)
sql = '''SELECT name
FROM (
SELECT DISTINCT a.name, b.number
FROM table1 as a
JOIN table2 as b ON a.sk = b.sk
WHERE a.name IN ({})
ORDER BY b.number
) sub
GROUP BY name
'''
qmarks = ", ".join(['?' for i in sample])
cursor.execute(sql.format(qmarks), sample)
for r in cursor.fetchall():
print(r)