简体   繁体   English

discord.py: MySQL '光标未连接'

[英]discord.py: MySQL 'Cursor is not connected'

I'm trying to store data in my local database before kicking a member of a guild.我试图在踢公会成员之前将数据存储在我的本地数据库中。 I have the database setup in another file.我在另一个文件中设置了数据库。 I'm calling the kick function in another file.我在另一个文件中调用了踢 function。 What I noticed is that sometimes it works and when I do it again it doesn't work anymore.我注意到有时它会起作用,而当我再次这样做时,它就不再起作用了。

My code:我的代码:

# Database.py

import os
import mysql.connector

from dotenv import load_dotenv

load_dotenv()


def modlog_db_connex():
    class ModLogDB:
        def __init__(self, connection, mycursor):
            self.connection = connection
            self.mycursor = mycursor

    modlog_db = mysql.connector.connect(
        host=os.getenv("DB_HOST"),
        user=os.getenv("DB_USERNAME"),
        password=os.getenv("DB_PASSWORD"),
        database=os.getenv("DB_NAME"),
        use_unicode=True
    )

    mycursor = modlog_db.cursor()
    return ModLogDB(modlog_db, mycursor)


# Kick.py

import discord

import GlobalSupport
import Database

moderator_above = GlobalSupport.moderator_above
modlog_db = Database.modlog_db_connex()


async def kick(client, ctx, member, reason):
  
      author_id = ctx.author.id
      log_channel = GlobalSupport.moderation_logs
      if any(role.id in moderator_above for role in ctx.author.roles) and author_id != int(member.id):

          if reason is None:
              reason = "None provided."

          sql = "INSERT INTO RLH_Cases_v2 (UserID, ModeratorID, CaseType, Reason) VALUES (%s, %s, %s, %s)"
          val = (str(member.id), str(ctx.author.id), "Kick", reason)
          modlog_db.mycursor.execute(sql, val)
          modlog_db.connection.commit()
          case_id = str(modlog_db.mycursor.lastrowid)
          modlog_db.connection.close()

          # ...

Error:错误:

Traceback (most recent call last):
  File "C:\Users\user\Desktop\Team Stuff (TS, DC, MC)\RLH\Programming\OnMessage.py Rewrite\Commands\Kick.py", line 31, in kick
    modlog_db.mycursor.execute(sql, val)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mysql\connector\cursor_cext.py", line 242, in execute
    raise errors.ProgrammingError("Cursor is not connected", 2055)
mysql.connector.errors.ProgrammingError: 2055: Cursor is not connected

You're closing the connection to your MySQL database after each kick and don't reconnect prior to calling kick again.每次启动后,您将关闭与 MySQL 数据库的连接,并且在再次调用 kick 之前不要重新连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM