本文介绍了准备好的sql语句中整数的Python列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题有点像 Python 字符串到 SQL 的列表IN 参数 但我有一个整数列表.我使用的python代码是:
My question is somewhat the same as Python list of String to SQL IN parameter but I have a list of integers. The python code I use is:
ids = [1000032, 1000048]
sql = 'SELECT CompNo, CompName, CompType FROM Component WHERE DeptID IN (?)'
cursor.execute(sql, [','.join(ids)])
Sybase 数据库引擎响应为:
And the Sybase database engine responds with:
pyodbc.Error: ('07006', "[07006] [Sybase][ODBC Driver][SQL Anywhere]Cannot convert '1000032','1000048' to a numeric (-157) (SQLExecDirectW)")
这样做的正确方法是什么?
What is the correct way to do this?
推荐答案
IMO 使用 str.format
ids = [1000032, 1000048]
sql = 'SELECT CompNo, CompName, CompType FROM Component WHERE DeptID IN ({0})'
sql = sql.format(','.join('?' * len(ids)))
cursor.execute(sql, (ids,))
...
这篇关于准备好的sql语句中整数的Python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!