程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了AttributeError: 模块 'mysql.connector' 没有属性 'cursor_cext'大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决AttributeError: 模块 'mysql.connector' 没有属性 'cursor_cext'?

开发过程中遇到AttributeError: 模块 'mysql.connector' 没有属性 'cursor_cext'的问题如何解决?下面主要结合日常开发的经验,给出你关于AttributeError: 模块 'mysql.connector' 没有属性 'cursor_cext'的解决方法建议,希望对你解决AttributeError: 模块 'mysql.connector' 没有属性 'cursor_cext'有所启发或帮助;

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

错误

AttributeError: module 'Mysql.connector' has no attribute 'cursor_cext'
@H_618_11@

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)
@H_618_11@

请告知我在这里做错了什么。我也认为我已经导入了正确的依赖项。

解决方法

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

from mysql.connector.connection import MySQLConnection

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

def connect_db(
        params: Dict,verbose: bool = True
    ) -> MySQLConnection:
@H_618_11@

大佬总结

以上是大佬教程为你收集整理的AttributeError: 模块 'mysql.connector' 没有属性 'cursor_cext'全部内容,希望文章能够帮你解决AttributeError: 模块 'mysql.connector' 没有属性 'cursor_cext'所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。