I apologise if similar has been posted before (it no doubt has) but I can't seem to find a question that has a similar issue. I am building a basic ticketing system bot using Python, SQL and Hangouts Chat. The Chat part isn't really relevant. At a core level, all I am trying to do is update a very basic SQL database with a Python Script. I can insert and view my records fine. Please note that I am also using this in a hangouts chat bot, so I apologise for that, as well as the fact my code is very 'wide open' at the moment, as I am debugging it by looking at each variable separately. I will neaten it all up when I am done.
elif "!update" in messagesentstr:
messagesentstrsplit = messagesentstr.split(" ", 3) #Takes string from Hangouts Chat message and splits it in to 4
messagesentstrsplitid = messagesentstrsplit[2] #Takes the row number to be updated from the chat message
myint = int(messagesentstrsplitid) #Converts the number to an int from string
mycursor.execute("SELECT issue FROM tickets WHERE id = %d" % (myint)) #Pulls the relevant record
myresult = mycursor.fetchone()
outputwithchar = json.dumps(myresult)
outputnochar = outputwithchar[2:-2]
updatedcol = outputnochar + ' ' + messagesentstrsplit[3]
mytuple = (updatedcol,)
sqlupdatecom = "UPDATE tickets SET issue = (%s) WHERE id = (%d)"
mycursor.execute(sqlupdatecom, mytuple, myint)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")
updatedcolmsg = 'The ticket with the ID ' + str(myint) + ' has been updated to: "' + updatedcol + '"'
texttoshow = (updatedcolmsg)
In an example, messagesentstr would equal '!update 12 I then did this.'. Row 12 would already have 'I have this issue.' under the issue column.
When run, I get '-1 record(s) affected' but no errors, and my record stays the same. I would expect to get 'I have this issue. I then did this.' for issue id 12.
Thanks
EDIT: Have noticed it works fine when I put a value in, instead of %s, so my string is probably not actually a string
For anyone with a similar issue, changing to:
sqlupdatecom = (
"UPDATE tickets SET issue = %s "
"WHERE id = %s")
mycursor.execute(sqlupdatecom, (updatedcol, myint))
mydb.commit()
fixed it for me