Why close a cursor for Sqlite3 in Python

青春壹個敷衍的年華 提交于 2019-12-21 17:41:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!