【问题标题】:Error when executing SQL statement in python [duplicate]在python中执行SQL语句时出错[重复]
【发布时间】:2017-11-03 10:28:39
【问题描述】:

下面的代码给了我一个错误:

db = sqlite3.connect(dbfile)
cursor = db.cursor()
cursor.execute("SELECT gsr, {} FROM {} WHERE session_id=?".format(column,table),(id))

其中columntable 是字符串,id 是数字。我收到 ValueError: parameters are unsupported type error。

为什么会这样?

【问题讨论】:

  • 你为什么不用"SELECT gsr, {} FROM {} WHERE session_id={}".format(column,table, id)

标签: python sqlite


【解决方案1】:

删除 () arround id:

db = sqlite3.connect(dbfile)
cursor = db.cursor()
cursor.execute("SELECT gsr, {} FROM {} WHERE session_id=?".format(column,table),id)

您想使用 int id 设置 ? 而不是元组 (id)

或使用命名占位符:

db = sqlite3.connect(dbfile)
cursor = db.cursor()
cursor.execute("SELECT gsr, {} FROM {} WHERE session_id=:id".format(column,table),{"id":id})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-24
    • 2021-10-05
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 2019-11-09
    相关资源
    最近更新 更多