加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

从sqlite表中选择rowid在列表中使用python sqlite3 – DB-API 2.

发布时间:2020-12-12 19:09:41 所属栏目:百科 来源:网络整理
导读:以下作品: cursor.execute("select * from sqlitetable where rowid in (2,3);") 以下不是: cursor.execute("select * from sqlitetable where rowid in (?) ",[[2,3]] )sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
以下作品:
>>> cursor.execute("select * from sqlitetable where rowid in (2,3);")

以下不是:

>>> cursor.execute("select * from sqlitetable where rowid in (?) ",[[2,3]] )
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.

有没有办法传递一个python列表,而不必先将它格式化成一个字符串?

不幸的是每个值都必须有自己的参数标记(?).
由于参数列表(可能)具有任意长度,因此您必须使用字符串格式来构建正确数量的参数标记.幸运的是,这不是很难:
args=[2,3]
sql="select * from sqlitetable where rowid in ({seq})".format(
    seq=','.join(['?']*len(args)))

cursor.execute(sql,args)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读