【问题标题】:I am unable to update my SQL database in Python, but can add new rows我无法在 Python 中更新我的 SQL 数据库,但可以添加新行
【发布时间】:2019-03-27 13:01:46
【问题描述】:

如果之前发布过类似的内容,我深表歉意(毫无疑问),但我似乎找不到有类似问题的问题。我正在使用 Python、SQL 和 Hangouts Chat 构建一个基本的票务系统机器人。聊天部分并不真正相关。在核心级别上,我要做的就是使用 Python 脚本更新一个非常基本的 SQL 数据库。我可以很好地插入和查看我的记录。请注意,我也在环聊聊天机器人中使用它,因此我为此道歉,以及我的代码目前非常“开放”的事实,因为我正在通过分别查看每个变量来调试它。完成后我会整理一下。

        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)

在一个例子中,messentstr 将等于'!update 12 I then did this.'。第 12 行已经有“我有这个问题”。在问题栏下。

运行时,我得到“-1 条记录受影响”但没有错误,并且我的记录保持不变。我希望得到'我有这个问题。然后我做了这个。对于问题 ID 12。

谢谢

编辑:注意到当我输入一个值而不是 %s 时它工作正常,所以我的字符串可能实际上不是一个字符串

【问题讨论】:

  • 当您在 UPDATE 语句中执行字符串格式时,您正在格式化元组值,将其更改为基本字符串,看看它是否有效。
  • @NishantPatel 已经完成了,仍然没有运气。将元组替换为基本字符串,仍然为 -1。

标签: python sql mysql-connector hangouts-chat


【解决方案1】:

对于有类似问题的任何人,更改为:

sqlupdatecom = (
                "UPDATE tickets SET issue = %s "
                "WHERE id = %s")
                mycursor.execute(sqlupdatecom, (updatedcol, myint))
                mydb.commit()

帮我修好了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多