python MySQLdb模块有些操作失效问题解决

267 篇文章 1 订阅
42 篇文章 0 订阅

 今天用这个模块做update操作时没有办法写入库中,原因是如果你使用InnoDB之类的存储引擎管理表的时候需要手动提交事务,否则所有的更改都变得无效。

  官方文档见http://mysql-python.sourceforge.net/FAQ.html#my-data-disappeared-or-won-t-go-away 

Starting with 1.2.0, MySQLdb disables autocommit by default, as required by the DB-API standard (PEP-249). If you are using InnoDB tables or some other type of transactional table type, you'll need to do connection.commit() before closing the connection, or else none of your changes will be written to the database.

Conversely, you can also use connection.rollback() to throw away any changes you've made since the last commit.

Important note: Some SQL statements -- specifically DDL statements like CREATE TABLE -- are non-transactional, so they can't be rolled back, and they cause pending transactions to commit.

 

解决方法有两种:

  一是在关闭数据库连接之前执行commit()操作

  二是初始化数据库连接的时候设置事务类型为自动提交事务autocommit(True)

如果你对编程感兴趣或者想往编程方向发展,可以关注微信公众号【筑梦编程】,大家一起交流讨论!小编也会每天定时更新既有趣又有用的编程知识!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值