V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
daiv
V2EX  ›  Python

Sqlite的游标 cursor,最后需要关闭吗?

  •  
  •   daiv · 2013-12-20 16:42:40 +08:00 · 5944 次点击
    这是一个创建于 3779 天前的主题,其中的信息可能已经有所发展或是发生改变。
    Sqlite数据库,在最后需要关闭数据库。以前也一直使用并以为直接关闭数据库就好了。
    但是,
    我在看文档的时候,发现官方文档 有些地方 会去关闭游标cursor。为什么需要关闭游标?

    --- --- --- --- --- --- 直接关闭数据库 --- --- --- --- --- ---
    import sqlite3
    conn = sqlite3.connect('example.db')
    cur = conn.cursor()
    ... ...
    conn.close()

    --- --- --- --- --- --- 关闭连接前,关闭游标cursor --- --- --- --- --- ---
    import sqlite3
    conn = sqlite3.connect(example'.db')
    cur = conn.cursor()
    ... ...
    cur.close()
    conn.close()


    这个有区别吗?
    查了资料,有些说需要,有些说不用。我一直没关闭cursor,目前还没有出什么大问题,有些小问题不知道是不是这个细节引起的
    文档:http://docs.python.org/2/library/sqlite3.html
    2 条回复    2021-02-25 11:40:08 +08:00
    wuqiangroy
        1
    wuqiangroy  
       2017-10-23 16:19:00 +08:00
    要关闭,占用资源。
    imgbed
        2
    imgbed  
       2021-02-25 11:40:08 +08:00
    @wuqiangroy 会占多少资源?如果占得不多,我觉得没必要关闭,不然可能 bug 更多
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1339 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:36 · PVG 07:36 · LAX 16:36 · JFK 19:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.