我有一个列表,其中包含我要从数据库表中检索的列的名称。 我的问题是如何使游标选择列表中指定的列。在将 nameList 包含在 select 语句中之前,我是否必须将其转换为字符串变量?谢谢
nameList = ['A','B','C','D',...]
with sqlite3.connect(db_fileName) as conn:
cursor = conn.cursor()
cursor.execute("""
select * from table
""")
最佳答案
只要你能确定你的输入被净化——避免SQL injection攻击——你可以这样做:
...
qry = "select {} from table;"
qry.format( ','.join(nameList) )
cursor.execute(qry)
如果您使用的是非常旧的 Python 版本,请改为:
...
qry = "select %s from table;"
qry % ','.join(nameList)
cursor.execute(qry)
关于python - 在 Python 中使用 SQLite3 选择多个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29687046/