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

 今天用这个模块做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)

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

你可能感兴趣的:(python MySQLdb模块有些操作失效问题解决)