sqlite in python

本文介绍了PySQLite中sqlite3模块的事务管理机制,默认情况下,在每次数据修改语句(如INSERT/UPDATE/DELETE/REPLACE)前隐式开启事务,并在非数据修改语句前隐式提交事务。此外还说明了如何正确使用commit方法确保数据对其他数据库连接可见。

在PySQLite中的文档 http://docs.python.org/library/sqlite3.html?highlight=pysqlite#controlling-transactions

 

By default, the sqlite3 module opens transactions implicitly before a Data Modification Language (DML) statement (i.e. INSERT/UPDATE/DELETE/REPLACE), and commits transactions implicitly before a non-DML, non-query statement (i. e. anything other than SELECT or the aforementioned).
默认情况下, sqlite3模块,在每次数据修改语句(DML)(比如 INSERT/UPDATE/DELETE/REPLACE)中,都隐含打开一个事务, 并在 非数据修改语句(比如 DELETE等) 之前, 隐含地提交这个事务

 

所以, python中, 只需要在在connection 被close前, commit一次就好了, 不要每次execute都commit

 
This method commits the current transaction. If you don’t call this method, anything you did since the last call to commit() is not visible from other database connections. If you wonder why you don’t see the data you’ve written to the database, please check you didn’t forget to call this method.

如果不调用commit(),数据对其他的进程的数据库连接是不可见的

如果执行insert语句后, 又执行了select, 那么就不必commit了,因为会自动commit一次

 

reference:http://zhiwei.li/text/2010/08/sqlite%E6%8F%92%E5%85%A5%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值