I have this code who is supposed to be a start to my class project, but every time I try to execute it, it says:
AttributeError: module 'mysql.connector' has no attribute 'cursor_cext'.
import mysql.connector
from mysql.connector.connection import MySQLConnection
def create_db(name: str) -> mysql.connector.cursor_cext.CMySQLCursor():
"""Esta função criar uma base de dados com o nome name"""
# PASSO 1: Criar uma conexão
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="123456",
)
# PASSO 2: Criar uma Base de Dados
c = mydb.cursor()
c.execute(f"CREATE DATABASE {name}")
print(type(c))
return c
def check_dbases() -> tuple:
"""Esta função verifica as bases de dados existentes"""
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="123456",
)
cursor = mydb.cursor()
cursor.execute("SHOW DATABASES")
return [x for x in cursor]
1 Answer 1
basically, In Python, you can use type hints to specify the expected types of function arguments and return values. Type hints are specified using the : character, followed by the type and the -> for the function.
So it would look like this:
def function_name(parameter: type) -> return_type:
# function body
your equivalent of the above return_type is invalid.
The code in the question has used: mysql.connector.cursor_cext.CMySQLCursor()
whereas is should either be blank (use python default) or use this mysql.connector.
And this is the reason for the error.
so the first method should look like this:
def create_db(name: str)-> mysql.connector:
"""Esta função criar uma base de dados com o nome name"""
# PASSO 1: Criar uma conexão
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="123456",
)
# PASSO 2: Criar uma Base de Dados
c = mydb.cursor()
c.execute(f"CREATE DATABASE {name}")
print(type(c))
return c
Here is a link to the docs for type hinting:
Comments
Explore related questions
See similar questions with these tags.
-> mysql.connector.cursor_cext.CMySQLCursor().