ApplePie
ApplePie

Reputation: 1185

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

I am getting the following error when I try to connect to my database:

error

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)

Please advise what I did wrong here. I also think I have imported the right dependencies.

Upvotes: 0

Views: 1550

Answers (1)

Miguel Pinheiro
Miguel Pinheiro

Reputation: 342

The connect_db method doesn't return the type you're saying it does. As you can see in it's code here it returns a mysql.connector.connection.MySQLConnection object and not a mysql.connector.connection_cext.CMySQLConnection. You can fix your code by importing the right class using:

from mysql.connector.connection import MySQLConnection

And then replacing the typing on your connect_db method to:

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

Upvotes: 2

Related Questions