问题
Is there any benefit to closing a cursor when using Python's sqlite3 module? Or is it just an artifact of the DB API v2.0 that might only do something useful for other databases?
It makes sense that connection.close() releases resources; however, it is unclear what cursor.close() actually does, whether it actually releases some resource or does nothing. The docs for it are unenlightening:
>>> import sqlite3
>>> conn = sqlite3.connect(':memory:')
>>> c = conn.cursor()
>>> help(c.close)
Help on built-in function close:
close(...)
Closes the cursor.
Note, this is a completely different question than Why do you need to create a cursor when querying a sqlite database?. I know what cursors are for. The question is about what the cursor.close() method actually does and whether there is any benefit to calling it.
回答1:
In the case of SQLite, there isn't much difference but the API for databases is not just for embedded databases but for all SQL databases.
For a DBMS, a cursor often implies a session in the client and sometimes on the server.
So, if you are not using a reference counting implementation of Python (such as CPython), then a lot of resources could be tied up until GC frees them.
来源:https://stackoverflow.com/questions/43927799/why-close-a-cursor-for-sqlite3-in-python