为什么要在Python中关闭Sqlite3的游标 [英] Why close a cursor for Sqlite3 in Python
问题描述
使用Python的 sqlite3模块?还是仅仅是 DB API v2.0 的产物对其他数据库有用吗?
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?
有意义的是 connection.close()释放资源;但是,目前尚不清楚 cursor.close()的实际作用,它实际上是释放一些资源还是什么都不做。
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.
请注意,这是与为什么需要创建游标? 。我知道游标的用途。问题在于cursor.close()方法的实际作用以及调用它是否有任何好处。
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.
推荐答案
在对于SQLite,没有太大区别,但是数据库API不仅适用于嵌入式数据库,而且适用于所有SQL数据库。
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.
对于DBMS,通常使用游标
For a DBMS, a cursor often implies a session in the client and sometimes on the server.
因此,如果您不使用Python的引用计数实现(例如CPython),则很多资源可能会被占用,直到GC释放它们。
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.
这篇关于为什么要在Python中关闭Sqlite3的游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!