![](/img/trans.png)
[英]SQLite3 (python) query to search multiple columns in one SELECT statement
[英]select multiple columns using SQLite3 in Python
我有一個列表,其中包含我要從數據庫中的表中檢索的列的名稱。 我的問題是如何使光標選擇列表中指定的列。 在將nameList包含在select語句中之前,是否必須將nameList轉換為字符串變量? 謝謝
nameList = ['A','B','C','D',...]
with sqlite3.connect(db_fileName) as conn:
cursor = conn.cursor()
cursor.execute("""
select * from table
""")
只要您確定您的輸入已被清理 - 以避免SQL注入攻擊 - 您可以執行以下操作:
...
qry = "select {} from table;"
qry.format( ','.join(nameList) )
cursor.execute(qry)
如果您使用的是舊版本的Python,請改為:
...
qry = "select %s from table;"
qry % ','.join(nameList)
cursor.execute(qry)
nameList = ["'A(pct)'",'B','C','D',...]
with sqlite3.connect(db_fileName) as conn:
cursor = conn.cursor()
cursor.execute("""
select {} from table
""".format(", ".join(nameList)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.