【问题标题】:SQL server loses connection after one minutreSQL 服务器在一分钟后失去连接
【发布时间】:2020-09-18 11:25:56
【问题描述】:

我正在将 Heroku 和 Clear DB 用于我制作的 python 脚本。如果在 Discord 中部署机器人后,脚本似乎可以工作,它会收集用户的信息并在收集到一定数量的条目后回复他们。但是,如果在一分钟内没有消息,我的终端会出现以下错误:

 File "C:\Users\Chase The Great\Anaconda3\lib\site-packages\mysql\connector\network.py", line 251, in recv_plain
    raise errors.InterfaceError(errno=2013)
mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query

有没有办法让连接再次激活或使连接无限期?

我正在使用模块 mysql.connector 与数据库通信。如果我的编写方式不准确,这是我的一些代码:

async def on_message(message):
    sql_users_info = "SELECT * FROM host_server.user_levels;"
    mycursor.execute(sql_users_info)
    user_info = mycursor.fetchall()
    user_names = []
    user_exp = []
    for i in user_info:
        user_names.append(i[0])
        user_exp.append(i[1])
    await update_data(user_names, message.author)
    await add_experience(user_names, message.author, user_exp)
    await level_up(user_names, message.author, message.channel)
    await client.process_commands(message)

【问题讨论】:

    标签: python heroku mysql-workbench discord cleardb


    【解决方案1】:

    我能够找出问题所在。不使用数据库时需要重新建立连接。在 await 命令中,您使用以下内容:

    mydb = mysql.connector.connect(
     host="localhost",
     user="yourusername",
     passwd="yourpassword")
     mycursor = mydb.cursor()
     mycursor.execute("YOUR SQL CODE")
    

    每次发送消息时您都需要这样做。

    【讨论】:

      猜你喜欢
      • 2018-03-12
      • 2021-12-26
      • 2017-09-21
      • 1970-01-01
      • 2015-12-29
      • 2020-05-06
      • 1970-01-01
      • 2020-04-06
      • 2015-02-13
      相关资源
      最近更新 更多