【问题标题】:Update hangs code -- Python MySQL Connector更新挂起代码——Python MySQL 连接器
【发布时间】:2019-03-18 05:59:57
【问题描述】:

所以我正在尝试更新一个表,由于某种原因,这段代码决定在执行时冻结。

import mysql.connector

cnx = mysql.connector.connect(user='user', password='password',
                              host='y u so interested',
                              database='discord')

cursor = cnx.cursor()

print ("Start")

update = ("UPDATE admin_daily_playtime_crp1 "
        "SET DiscordName = %s "
        "WHERE SteamName = %s ")
values = ("true", "Modern Mo")
cursor.execute(update, values)
cnx.commit()

print ("done")

表设置: https://gyazo.com/dde9475d33056b26c04d564e3e8f7349

【问题讨论】:

    标签: python mysql sql-update mysql-connector freeze


    【解决方案1】:

    考虑更新您的表以包含其中每个列的正确数据类型。我更改了您的代码片段以包含组织功能。调用函数 discordName(Discord Name, Steam Name) 应该会更新信息。我在自己的数据库上对此进行了测试,效果很好。

    import mysql.connector
    
    
    
    def connection():
        connection = mysql.connector.connect(user='root', password='', host='',database='discord')
    
        return connection
    
    def discordName(discordName, steamName):
        con = connection()
        cursor = con.cursor()
        print ("Start")
        update = "UPDATE `admin_daily_playtime_crp1` SET `DiscordName` = %s WHERE `SteamName` = %s"
        cursor.execute(update, (discordName, steamName))
        print (cursor.rowcount, "record(s) affected!")
        con.commit()
    

    如果您还有其他问题,请询问! 这是我使用的新表的图片https://gyazo.com/58dfd93e68ab4d452c896918f8ac2c8a

    【讨论】:

    • 我不明白,不知何故它不再挂起。虽然您的代码确实有效,但非常感谢您的帮助!
    • 可能是数据库连接问题什么的。还要尝试使变量名称对他们所做的事情很明显,这样其他人就可以更容易地阅读代码,而无需尝试翻译它。并不是说你的代码不好,但我想你写的越多就越难理解。
    • 在我的情况下仍然冻结,没有任何回溯。 -1
    猜你喜欢
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2016-07-19
    • 1970-01-01
    相关资源
    最近更新 更多