Reputation: 5389
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