在Python中使用SQLite3选择多个列

2024-04-27 05:02:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个列表,其中包含要从数据库表中检索的列的名称。 我的问题是如何让光标选择列表中指定的列。在将nameList包含在select语句中之前,是否必须将其转换为字符串变量?谢谢

nameList = ['A','B','C','D',...]

 with sqlite3.connect(db_fileName) as conn:
        cursor = conn.cursor()
        cursor.execute("""
        select * from table
        """)

Tags: 字符串名称数据库列表dbconnectwith语句
2条回答
nameList = ["'A(pct)'",'B','C','D',...]

 with sqlite3.connect(db_fileName) as conn:
        cursor = conn.cursor()
        cursor.execute("""
        select {} from table
        """.format(", ".join(nameList)))

{只要您能确保您的输入^被清理,就可以避免攻击:

    ...
    qry = "select {} from table;"
    qry.format( ','.join(nameList) )
    cursor.execute(qry)

如果您使用的是非常旧的Python版本,请执行以下操作:

^{pr2}$

相关问题 更多 >