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