sqlite 查询中的 python 字符串替换
我正在尝试使用 IN 语句返回与字符串列表之一匹配的结果,
例如
strings = ['string1', 'string2', 'string3']
c.execute('select count(*) from table where foo in ?', strings)
我知道这是不正确的并且不起作用,但我希望它突出显示我正在尝试做的事情...
i'm trying to use an IN statement to return results which match one of a list of strings
for example
strings = ['string1', 'string2', 'string3']
c.execute('select count(*) from table where foo in ?', strings)
i know this is incorrect and doesnt work but i hope that it highlights what im trying to do...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能那样做。存在三个问题:
table
的表。试试这个:
如果字符串的数量是可变的,请使用这个:
You can't do that. There are three problems:
table
unless you use backticks around the table name.Try this instead:
If the number of strings is variable, use this instead:
您可以按照 @Mark Byers 的建议执行
','.join(strings)
,这在大多数情况下都有效。但是,如果字符串数量非常长,则会失败,因为 SQL 查询的长度有限。另一种方法是创建一个临时表,在其中插入所有字符串并进行连接以执行交集,类似于
You can do a
','.join(strings)
as @Mark Byers suggests, that works most times. However if the number of strings is very long it will fail because SQL queries have bounded length.Another way of doing it is creating a temp table, inserting there all the strings and doing a join to perform the intersection, something like