sqlite3事务总结:

在connect()中不传入 isolation_level

事务处理:

使用connection.commit()

#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''sqlite3事务总结:
在connect()中不传入 isolation_level
事务处理:
使用connection.commit() 分析:
智能commit状态:
生成方式: 在connect()中不传入 isolation_level, 此时isolation_level==''
在进行 执行Data Modification Language (DML) 操作(INSERT/UPDATE/DELETE/REPLACE)时, 会自动打开一个事务,
在执行 非DML, 非query (非 SELECT 和上面提到的)语句时, 会隐式执行commit
可以使用 connection.commit()方法来进行提交
注意:
不能和cur.execute("COMMIT")共用 自动commit状态:
生成方式: 在connect()中传入 isolation_level=None
这样,在任何DML操作时,都会自动提交
事务处理
connection.execute("BEGIN TRANSACTION")
connection.execute("COMMIT")
如果不使用事务, 批量添加数据非常缓慢 数据对比:
两种方式, 事务耗时差别不大
count = 100000
智能commit即时提交耗时: 0.621
自动commit耗时: 0.601
智能commit即时提交耗时: 0.588
自动commit耗时: 0.581
智能commit即时提交耗时: 0.598
自动commit耗时: 0.588
智能commit即时提交耗时: 0.589
自动commit耗时: 0.602
智能commit即时提交耗时: 0.588
自动commit耗时: 0.622
''' import sys
import time class Elapse_time(object):
'''耗时统计工具'''
def __init__(self, prompt=''):
self.prompt = prompt
self.start = time.time() def __del__(self):
print('%s耗时: %.3f' % (self.prompt, time.time() - self.start))
CElapseTime = Elapse_time import sqlite3 # -------------------------------------------------------------------------------
# 测试
# filename = 'e:/temp/a.db' def prepare(isolation_level = ''):
connection = sqlite3.connect(filename, isolation_level = isolation_level)
connection.execute("create table IF NOT EXISTS people (num, age)")
connection.execute('delete from people')
connection.commit()
return connection, connection.cursor() def db_insert_values(cursor, count):
num = 1
age = 2 * num while num <= count:
cursor.execute("insert into people values (?, ?)", (num, age))
num += 1
age = 2 * num def study_case1_intelligent_commit(count):
'''
在智能commit状态下, 不能和cur.execute("COMMIT")共用
'''
connection, cursor = prepare() elapse_time = Elapse_time(' 智能commit')
db_insert_values(cursor, count)
#cursor.execute("COMMIT") #产生异常 cursor.execute("select count(*) from people")
print (cursor.fetchone()) def study_case2_autocommit(count):
connection, cursor = prepare(isolation_level = None) elapse_time = Elapse_time(' 自动commit')
db_insert_values(cursor, count) cursor.execute("select count(*) from people")
print (cursor.fetchone()) def study_case3_intelligent_commit_manual(count):
connection, cursor = prepare() elapse_time = Elapse_time(' 智能commit即时提交')
db_insert_values(cursor, count)
connection.commit() cursor.execute("select count(*) from people")
print (cursor.fetchone()) def study_case4_autocommit_transaction(count):
connection, cursor = prepare(isolation_level = None) elapse_time = Elapse_time(' 自动commit')
connection.execute("BEGIN TRANSACTION;") # 关键点
db_insert_values(cursor, count)
connection.execute("COMMIT;") #关键点 cursor.execute("select count(*) from people;")
print (cursor.fetchone()) if __name__ == '__main__':
count = 10000
prepare()
for i in range(5):
#study_case1_intelligent_commit(count) #不提交数据
#study_case2_autocommit(count) #非常缓慢
study_case3_intelligent_commit_manual(count)
study_case4_autocommit_transaction(count)

python-sqlite3事务的更多相关文章

  1. python sqlite3 数据库操作

    python sqlite3 数据库操作 SQLite3是python的内置模块,是一款非常小巧的嵌入式开源数据库软件. 1. 导入Python SQLite数据库模块 import sqlite3 ...

  2. python sqlite3使用

    python sqlite3文档地址:http://docs.python.org/2/library/sqlite3.html The sqlite3 module was written by G ...

  3. python sqlite3 入门 (视频讲座)

    python sqlite3 入门 (视频讲座) an SQLite mini-series! - Simple Databases with Python 播放列表: YouTube https:/ ...

  4. python sqlite3简单操作

    python sqlite3简单操作(原创)import sqlite3class CsqliteTable: def __init__(self): pass def linkSqlite3(sel ...

  5. python使用上下文管理器实现sqlite3事务机制

    如题,本文记录如何使用python上下文管理器的方式管理sqlite3的句柄创建和释放以及事务机制. 1.python上下文管理(with) python上下文管理(context),解决的是这样一类 ...

  6. 基于Python+Sqlite3实现最简单的CRUD

    一.基本描述 使用Python,熟悉sqlite3的基本操作(查插删改),以及基本数据类型.事务(ACID).     准备工作:在sqlite3的官网上下载预编译的sqlite文件(windows) ...

  7. python+sqlite3

    一个小例子, # -*- coding:utf-8 -*- ''' Created on 2015年10月8日 (1.1)Python 2.7 Tutorial Pt 12 SQLite - http ...

  8. [Python]sqlite3二进制文件存储问题(BLOB)(You must not use 8-bit bytestrings unless you use a text_factory...)

    事情是这种: 博主尝试用Python的sqlite3数据库存放加密后的usernamepassword信息,表是这种 CREATE TABLE IF NOT EXISTS user ( userID ...

  9. xls===>csv tables===via python ===> sqlite3.db

    I've got some files which can help a little bit to figure out where people are from based on their I ...

  10. Python MySQL事务、引擎、索引及第三方库sqlalchemy

    本节内容 1.数据库介绍2.事务3.引擎4.索引5.ORM sqlalchemy 1.数据库介绍 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一 ...

随机推荐

  1. 使用pelican创建静态博客

    创建工作目录 首先使用pip安装pelican和markdown pip install pelican markdown 然后创建目录 mkdir my_blog 接着进入目录cd my_blog, ...

  2. 连接(JOIN)运算

    内连接--INNER JOIN 此处用商品表(product)和商店商品表(ShopProduct)测试,外键:product_id select sp.shop_id, sp.shop_name, ...

  3. element ui table(表格)点击一行展开

    element ui是一个非常不错的vue的UI框架,element对table进行了封装,简化了vue对表格的渲染. element ui表格中有一个功能是展开行,在2.0版本官网例子中,只可以点击 ...

  4. vue中eventbus 多次触发的问题

    相比于react,vue是一个更上手很快的框架.所说简单易用,但在做Vue项目的过程中,就会发现,坑还是有的.组件之间传递数据是一个常见的需求,父子组件传递数据在官网有介绍,除了父子组件,一般地,任意 ...

  5. luogu P1856 [USACO5.5]矩形周长Picture 扫描线 + 线段树

    Code: #include<bits/stdc++.h> #define maxn 200007 #define inf 100005 using namespace std; void ...

  6. nyoj23-取石子(一)

    取石子(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 一天,TT在寝室闲着无聊,和同寝的人玩起了取石子游戏,而由于条件有限,他/她们是用旺仔小馒头当作石子.游戏的 ...

  7. 29.es路由原理

    主要知识点 1.document路由到shard的理解及原理 2.路由算法:shard = hash(routing) % number_of_primary_shards 3.routing值(_i ...

  8. java的几种对象(PO,VO,DAO,BO,POJO)解释 (转)

    java的几种对象(PO,VO,DAO,BO,POJO)解释 一.PO:persistant object 持久对象,可以看成是与数据库中的表相映射的java对象.最简单的PO就是对应数据库中某个表中 ...

  9. VirtualBox没有权限访问共享文件夹

    将用户添加至vboxsf组 命令: sudo adduser ly vboxsf 搞定!

  10. HDU - 4074 - Sum

    先上题目: Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...