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
nine9
V2EX  ›  Python

为什么 Python 对 MySQL 进行数据的插入之后需要 commit,数据库才会真的有数据?

  •  
  •   nine9 · 2019-04-20 20:09:07 +08:00 · 3653 次点击
    这是一个创建于 1804 天前的主题,其中的信息可能已经有所发展或是发生改变。
    MySQL 使用的存储引擎是 InnoDB, 且 autocommit=ON,使用 python 包 MySQLdb
    6 条回复    2019-04-20 23:52:15 +08:00
    starsky007
        1
    starsky007  
       2019-04-20 20:11:27 +08:00 via Android
    开了事务?
    nine9
        2
    nine9  
    OP
       2019-04-20 20:14:01 +08:00
    autocommit=ON 不是意味着自动 commit 吗?
    starsky007
        3
    starsky007  
       2019-04-20 20:21:50 +08:00 via Android
    http://www.qttc.net/201208175.html

    MySQL 默认操作模式就是 autocommit 自动提交模式。这就表示除非**显式地开始一个事务**,否则每个查询都被当做一个单独的事务自动执行。
    dbit
        4
    dbit  
       2019-04-20 20:43:47 +08:00   ❤️ 1
    autocommit 是按会话设置, 说不定你用的 python 执行默认的是 autocommit=false
    nine9
        5
    nine9  
    OP
       2019-04-20 21:40:05 +08:00
    @dbit 是的,我一开始的思路也是这样,但是网上搜索了很多并没有找到答案,不过现在自己解决了。
    autocommit = kwargs2.pop('autocommit', False)
    rainmakeroly
        6
    rainmakeroly  
       2019-04-20 23:52:15 +08:00 via Android   ❤️ 1
    conn.autocommit(True)
    SO 上说是你用的库默认 false
    可以先看下官方文档
    autocommit 是个方法
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3092 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 12:53 · PVG 20:53 · LAX 05:53 · JFK 08:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.