>> cursor.execute-6ren">
gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:00:12 24 4
gpt4 key购买 nike

以下作品:

>>> 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)

关于python - 使用 python sqlite3 从 sqlite 表中选择 rowid 在列表中 - DB-API 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5766230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com