kovac
kovac

Reputation: 5389

Cost of mysql.connector.connect and connection.cursor

I'm new to Python. I have to run queries in MySQL using Python mysql driver mysql.connector. What I want to know is the relative costs of opening connection and getting cursor because I need to do this in a loop. Currently, I'm opening connection outside the loop and getting cursor inside the loop:

def example(self, array):
        cnx = mysql.connector.connect()

        for item in array:
            cursor = cnx.cursor()  

            query = ("dummy query")
            cursor.execute(query)

            result = cursor.fetchall()
            # do something here...

            cursor.close()

        cnx.close()

I want to know if cnx.cursor() is an expensive operation. Thanks.

Upvotes: 1

Views: 464

Answers (1)

Sherry
Sherry

Reputation: 189

connection is expensive, but cursor is not. what you do is correct.

Upvotes: 2

Related Questions