以下工程:
>>> 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列表,而不必先将其格式化为字符串?
4条答案
按热度按时间qxsslcnc1#
很遗憾没有每个值都必须有自己的参数标记(
?
)。由于参数列表可以(假定)具有任意长度,因此必须使用字符串格式来构建正确数量的参数标记。幸运的是,这并不难:字符串
lhcgjxsq2#
在Python 3.6中,你也可以用f字符串构建查询:
字符串
rqdpfwrv3#
SQLite本身只支持TEXT、INTEGER、真实的、BLOB和NULL类型。如果你想使用其他类型,你必须自己添加对它们的支持。detect_types参数和使用在模块级register_converter()函数中注册的自定义转换器使您可以轻松地完成此操作。
如前所述,SQLite本机仅支持有限的类型集。
要在SQLite中使用其他Python类型,必须使它们适应sqlite3模块支持的SQLite类型之一:NoneType、int、float、str、bytes之一。
https://docs.python.org/3.6/library/sqlite3.html#using-adapters-to-store-additional-python-types-in-sqlite-databases
wz1wpwve4#
我需要在查询中包含多个
IN
子句沿着其他命名参数,并提出了一个帮助方法,可以让我做到:字符串
它使用这个helper方法:
型