【问题标题】:SQL Query formatting Error: unsupported format character ',' (0x2c) at index 286")) in pythonSQL查询格式化错误:python中不支持的格式字符','(0x2c)在索引286“))
【发布时间】:2021-08-12 08:25:51
【问题描述】:

我正在 python 代码中执行 SQL 查询。此查询在 SQLite 的 DB 浏览器中使用时可以正常工作,当我尝试在 python 中使用时,它显示如下错误:

 Error: unsupported format character ',' (0x2c) at index 286")) in python .

这里查询:

query = "Select * from ( select detection_class, detection_class_id, matched_alert_id, stream_id, track_id, detection_time, frame_id "\
                " from matched_alert "\
                " where stream_id = '863b1675269e4600ab52a2cf0c418edc' "\
                " ) "\
                " where (SELECT ',' || detection_class || ',' FROM alerts WHERE alert_id=4 ) LIKE  '%,' || detection_class || ',%' "

执行如下:

        place_holders = [stream_id, alert_id]  
        try:
            with connection.cursor() as cursor:
                cursor.execute(query, place_holders)
                rows = cursor.fetchall()

我尝试了相关问题的各种答案,但没有奏效。 我该如何解决这个问题,有人建议我吗?

【问题讨论】:

    标签: python sql sqlite


    【解决方案1】:

    我得到了答案,伙计们,在格式字符串中 % 引入了像 %s 这样的格式字符 - 但是在您有 '%,'',%' 的地方,, 不是有效的格式字符。所以,为此我使用 '%,' 而不是 '%%,'',%%' 并且效果很好。

    对于 mero 描述的答案在这个问题中

    Python -- unsupported format character ',' (0x2c) at index 62 while inserting data into DB

    【讨论】:

    • 在构造查询时,您永远不应该使用 %s 占位符来传递参数。格式字符串不安全。始终使用 ? 占位符。
    • 是的,兄弟,非常感谢您的帮助。我从没见过像你这样胸怀大志的人。你很有帮助,谢谢...
    猜你喜欢
    • 1970-01-01
    • 2021-08-16
    • 2011-07-11
    • 2014-06-18
    • 2023-03-29
    • 1970-01-01
    • 2021-06-06
    • 2017-08-22
    • 2012-07-26
    相关资源
    最近更新 更多