0

I m unable to connect to my database and it gives this error that object is not callable. I did search up the error and even what it means. I changed a lots of stuff in the code but it didn't work for me. like instead of using the import mysql.connector.connect() to connect to the database I used connection.MySQLConnection() and it still gave the same error. I even changed the variable names because I saw some where that variables could also be an issue some time. It didn't help either, the problem might not be big but I m just having a hard time to fix it.

def Database_connection(self):
        
        dbhost = "localhost"
        dbuser = "test"
        dbpass = "1234"
        database_name ="school"
        
        #CONNECTION OF DB
        self.mdb = mysql.connector.connect(
            
            host = dbhost,
            password = dbpass,
            user = dbuser,

        )
        #Cursor to mysql database
        cursor = self.mdb.cursor()
        
        #Creates database if it doesn't exists
        cursor.execute(f'CREATE DATABASE IF NOT EXISTS {database_name}')
        
        self.mdb = mysql.connector.connect(
            
            host = dbhost,
            password = dbpass,
            user = dbuser,
            db = database_name,
        )
        return self.mdb


def insert_new_student(self):
        
        try:
            conn = self.Database_connection()

            if conn is None:
                return
            
            cursor = conn().cursor()

            conn.commit()
            cursor.close()
            conn.close()
 
        except mysql.connector.Error as err: #Handles Error

            print(f"Error: {err}")

The Error this code generates is:

line 380, in insert_new_student
    cursor = conn().cursor()
             ^^^^^^
TypeError: 'MySQLConnection' object is not callable
4
  • Which one is line 380? My guess is that you need to remove () after conn in conn().cursor() Commented Sep 11, 2024 at 19:47
  • cursor = conn.cursor(). That’s aside from some other scary lines in the code. Was this a copy/paste job or a YouTube ‘tutorial’? I’d strongly recommend understanding what each line is doing; and this may help you understand where you can clean up the scary bits. Commented Sep 11, 2024 at 19:55
  • Thanks it worked. @s3dev I just started learning python so that's my code looks like a youtube tutorial XD Commented Sep 11, 2024 at 20:09
  • Instead of cursor = conn().cursor() use conn = self.Database_connection(), You must have copied code from somewhere but forgot to rename the function name. Upvoted you so that you are still able to look for help on this forum Commented Sep 12, 2024 at 4:19

1 Answer 1

0

I think the self.mdb is the connection object, so the cursor would be conn.cursor.

Line 380: conn.cursor()

The reason is that an object can not be called, however, it's method can be called and conn in a connection object, yes?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.