I'm trying to create a function that will handle API error messages but I get this error message in Python:
Exception has occurred: sqlite3.OperationalError
near "Test4": syntax error
The server response is:
{"message":"Failed to validate one or more request parameters","validationErrors":["Budget name must be unique. 'Test4 - X4574747-PHONE' already exits"]}
And my code is:
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)
I can't put my finger on what causes this error. Any help would be appreciated. Thank you.
logTextvalue in case ofvalidationErrors. I guess there is a problem with quotes. Btw best way to insert str into str with quotes is using%r. Likesql = "insert into log values(%r)" % logText.c.execute("INSERT INTO log VALUES (?)", [logText])'quote beforeTest4. Use the way Dan D. showed.