【问题标题】:sqlite select query including a "VALUES" in the WHERE clause returns correctly with sqlite but not with python sqlite3在 WHERE 子句中包含“VALUES”的 sqlite 选择查询可以使用 sqlite 正确返回,但不能使用 python sqlite3
【发布时间】:2017-07-27 03:46:56
【问题描述】:

我有以下 SQL 查询,它从表“my_table”中选择列“item”和“feature”,其中“item”和“other_feature”这对列与某些值对匹配。

select item, feature from my_table 
where (item, other_feature) in (VALUES ('item1', 'A'), ('item1', 'B'));

Here is an example of the dataset used.

此查询在 sqlite3 命令行界面中按预期工作。但是,使用相同的数据库,当使用以下代码在 python 中使用sqlite3 模块时出现错误。

import sqlite3
query = "select item, feature from my_table where (item, other_feature) in (VALUES ('item1', 'A'), ('item1', 'B'))"
conn = sqlite3.connect("data/my_database.db")
conn.execute(query)

我收到以下错误:

sqlite3.OperationalError: near ",": syntax error

为什么这个查询在 python sqlite3 模块中不能正常工作?

【问题讨论】:

    标签: python sqlite


    【解决方案1】:

    尝试使用 " 引用您的字符串:

    query = 'select item, feature from my_table where (item, other_feature) in (VALUES ("item1", "A"), ("item1", "B"))'
    

    您可能还想检查 sqlite 版本

    select sqlite_version();
    

    或者在python中

    import sqlite3
    sqlite3.sqlite_version
    

    你们的 python 发行版不低于你的命令行界面,特别是如果它早于 3.15.2。

    【讨论】:

    • 不走运,我在引用 " 时遇到同样的错误。
    • 确实与sqlite3.sqlite_version 3.13.0 版我有这个问题,它适用于3.16.2。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多