推荐学习书目
› 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
myyou
V2EX  ›  Python

python mysqldb 使用完需要关闭吗?

  •  
  •   myyou · Apr 11, 2016 · 5654 views
    This topic created in 3858 days ago, the information mentioned may be changed or developed.

    在 python 中新建连接 con=MySQLdb.connect("127.0.0.1","root","password","database")获取游标 cursor=con.cursor() 在 con.commit()结束后需要关闭运行 con.close()还是只需要运行 cursor.close()关闭游标不关闭连接?

    9 replies  •  2016-04-12 10:12:44 +08:00
    peter999
    peter999
    Apr 11, 2016❤️1
    1
    cursor.close()
    con.close()
    icedx
    icedx
    Apr 11, 2016❤️1
    2
    单线程环境不建议频繁 con.close()
    多线程请交给 ORM 框架来做
    myyou
    myyou
    OP
    Apr 11, 2016
    3
    @peter999 con.close()关闭后游标不是会自动关闭吗?
    myyou
    myyou
    OP
    Apr 11, 2016
    4
    @icedx 单线程环境是不是只需要关闭游标啊?
    peter999
    peter999
    Apr 11, 2016❤️1
    5
    教程上是这么写的,我一般用连接池,很少直接用链接了
    icedx
    icedx
    Apr 11, 2016❤️1
    6
    @myyou 是的
    myyou
    myyou
    OP
    Apr 12, 2016
    7
    @peter999 你说的连接池是 PooledDB 吗?这个连接池需要关闭吗?
    peter999
    peter999
    Apr 12, 2016❤️1
    8
    需要,不过不是关闭数据库,只是释放回连接池。如果使用 django 这种自带 orm 的,就不需要 close 了
    myyou
    myyou
    OP
    Apr 12, 2016
    9
    @peter999 明白了谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1429 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 18:24 · PVG 02:24 · LAX 10:24 · JFK 13:24
    ♥ Do have faith in what you're doing.