Essentials: close and commit
In programs that interface with multiple databases or otherwise persist beyond the database connection that you have initiated, you will find a need to use a couple of MySQL commands that we have not yet discussed: close
and commit
.
In need of some closure
When one is finished with a database, it is good practice to close the cursor proxy. This ensures the cursor is not used again to refer to that database connection and also frees up resources. To close a cursor connection in MySQL for Python, simply issue the method call to your cursor object:
cur.close()
What happened to commit?
If you are experienced with using the MySQL shell or perhaps programming interfaces with MySQL using different APIs, you may wonder what has happened to the commit
call that one normally would make at the end of every transaction to render changes permanent.
MySQL for Python ships with an autocommit feature. Therefore, when the connection is closed, the changes are committed. However, if...