Python/Sqlite3:发生异常:Sqlite3.operational

2024-09-30 05:33:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个函数来处理API错误消息,但是我在Python中得到了这个错误消息:

Exception has occurred: sqlite3.OperationalError
near "Test4": syntax error

服务器响应为:

^{2}$

我的代码是:

def error():
    if "message" in r.json():
        logText = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + " : " + r.json()['message']
        c.execute("INSERT INTO log VALUES ('"+ logText +"')")
        conn.commit()
        if "validationErrors" in r.json():
            logText = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + " : " + r.json()['validationErrors'][0]
            c.execute("INSERT INTO log VALUES ('"+ logText +"')")
            conn.commit()
        os._exit(1)

我不知道是什么导致了这个错误。任何帮助都将不胜感激。非常感谢。在


Tags: inlogjson消息messageexecuteiftime
1条回答
网友
1楼 · 发布于 2024-09-30 05:33:22
logText = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + " : " + r.json()['validationErrors'][0]
c.execute("INSERT INTO log VALUES ('"+ logText +"')")

您发送的是这个SQL INSERT INTO log VALUES ('2018-12-10 23:31:26 : Budget name must be unique. 'Test4 - X4574747-PHONE' already exits'),正如您看到的,您在Test4之前关闭了'引号,这就是为什么SQL不理解结束引号之后发生了什么。在

使用

Dan's code works, but I don't understand it.

?表示从给定参数列表传递参数。它是[logText]。最好使用这种方法来避免SQL注入。在

here

相关问题 更多 >

    热门问题