ellaRT
ellaRT

Reputation: 1366

Reopen Mysql Connection in Python

just wanna ask how do you reopen the connection after closing it?

i have this script

cursor.execute(add_data, dummy_data)
cnx = mysql.connector.connect(user='',
                                password ='',host='',
                              database='')
cursor = cnx.cursor()

add_data = insert statement
dummy_data = dictionary of data


cursor.execute(add_data, dummy_data)

cnx.commit()
cnx.close()

After inserting one set of values i cannot insert anymore.

This is the error : "OperationalError: 2055: Lost connection to MySQL server at system error 9: Bad file descriptor"

thanks in advance!

Upvotes: 2

Views: 3551

Answers (1)

Saher Ahwal
Saher Ahwal

Reputation: 9237

You need a new connection object after you close

cnx2 = mysql.connector.connect(user='',
                                password ='',host='',
                              database='')

but why would you close the connection? you should close the cursor and keep the connection open for all operations.

I would define an open connection function and a close connection function that I will call at begginning and end of program respectively.

Upvotes: 1

Related Questions