AttributeError: module 'mysql.connector' has no attribute 'cursor_cext'

AttributeError: module 'mysql.connector' has no attribute 'cursor_cext'

当我尝试连接到我的数据库时出现以下错误:

错误

AttributeError: module 'mysql.connector' has no attribute 'cursor_cext'

code.py

    import mysql
    import mysql.connector  
    from mysql.connector import errorcode 
    
    
    # MySQL database access parameters
    DB_PARAMS = {
        "user": "root",
        "password": "letmego",
        "host": "localhost",
        "port": 3306,
        "raise_on_warnings": False,
        "database": "playground"
    }
    
    
    def connect_db(
        params: dict,
        verbose: bool = True
    ) -> mysql.connector.connection_cext.CMySQLConnection:
    
        try:
        conn = mysql.connector.connect(**params)
    except mysql.connector.Error as err:
        print("[!] Function 'connect_db'")
        print(f"\t{err}\n")
        exit(1)
    else:
        if verbose:
            print(f"Database '{params['database']}' successfully connected.\n")
        return conn

if __name__ == '__main__':

    # connect to MySQL
    conn = connect_db(DB_PARAMS)

请指教我这里做错了什么。我还认为我已经导入了正确的依赖项。

connect_db 方法并不 return 您所说的类型。正如您在它的 code here 中看到的那样,它 return 是一个 mysql.connector.connection.MySQLConnection 对象而不是 mysql.connector.connection_cext.CMySQLConnection。您可以使用以下方法导入正确的 class 来修复您的代码:

from mysql.connector.connection import MySQLConnection

然后将 connect_db 方法中的输入替换为:

def connect_db(
        params: dict,
        verbose: bool = True
    ) -> MySQLConnection: