2

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!

1
  • Normally, we close it after we've done all the inserts/updates/selects/... Commented Jan 6, 2015 at 1:59

1 Answer 1

1

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.

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

3 Comments

but python doesnt have an open connection function unlike other languages, so i just need to declare a new one after closing the cursor?
yes that is what I meant actually the connect function is the open
you're welcome. If you think this answered your question well, please accept it and/or upvote. Thanks you!

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.