0

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]
jarlh
44.9k8 gold badges52 silver badges68 bronze badges
asked Jan 20, 2023 at 18:23
1
  • you can delete this and it will run -> mysql.connector.cursor_cext.CMySQLCursor(). Commented Jan 20, 2023 at 18:52

1 Answer 1

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:

https://docs.python.org/3/library/typing.html

answered Jan 20, 2023 at 18:59
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.