attributeerror:模块“mysql.connector”没有属性“cursor\u cext”

lvjbypge  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(430)

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

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

代码.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)

请告诉我我做错了什么。我还认为我已经导入了正确的依赖项。

jutyujz0

jutyujz01#

这个 connect_db 方法不会返回您所说的类型。正如您在这里的代码中看到的,它返回一个 mysql.connector.connection.MySQLConnection 对象而不是 mysql.connector.connection_cext.CMySQLConnection . 您可以通过以下方式导入正确的类来修复代码: from mysql.connector.connection import MySQLConnection 然后将connect_db方法上的键入替换为:

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

相关问题