我正在尝试创建一个函数来处理 API 错误消息,但我在 Python 中收到此错误消息:
Exception has occurred: sqlite3.OperationalError
near "Test4": syntax error
服务器响应是:
{"message":"Failed to validate one or more request parameters","validationErrors":["Budget name must be unique. 'Test4 - X4574747-PHONE' already exits"]}
我的代码是:
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)
我无法确定导致此错误的原因。任何帮助,将不胜感激。谢谢你。
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 不了解关闭引号后发生了什么的原因。
用 c.execute("INSERT INTO log VALUES (?)", [logText])
丹的代码有效,但我不明白。
?
表示从给定参数列表传递参数。这是[logText]
. 最好使用这种方式来避免 SQL 注入。
看这里
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句