Python sqlite3事务处理方法实例分析
作者:一花一世界,一叶一乾坤 发布时间:2022-12-28 07:30:09
标签:Python,sqlite
本文实例讲述了Python sqlite3事务处理方法。分享给大家供大家参考,具体如下:
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程序设计有所帮助。


猜你喜欢
- js 获取经纬度的实现方法<!-- copyright (c) 2009 Google inc. You are free to
- 最近在做一个游戏数据统计后台,最基础的功能是通过分析注册登录日志来展示用户数据。在公司内部测试,用户量很少,所以就没有发现什么性能问题。但是
- 先来看看python的版本: >>> import sys >>> sys.version &a
- 今天先聊一聊在windows/mac iOS系统下用venv搭建python轻量级虚拟环境的问题。使用venv搭建的虚拟环境同virtual
- sql不常用函数总结以及事务,增加,删除触发器 distinct 删除重复行 declare @x 申明一个变量 convert(varch
- WEB程序员的技能要求: Web程序员不光要对 后端程序(如 php,jsp,asp.net)语言知识,对程序设计架构知识,数据库
- html文件中引入js文件,显示乱码!js文件为utf-8 编码(无bom) ,此时只要将js文件转成utf-8 BOM编码就可
- 本文实例讲述了Python装饰器原理与基本用法。分享给大家供大家参考,具体如下:装饰器:意义:在不能改变原函数的源代码,和在不改变整个项目中
- 堆是一棵完全二叉树。堆分为大根堆和小根堆,大根堆是父节点大于左右子节点,并且左右子树也满足该性质的完全二叉树。小根堆相反。可以利用堆来实现优
- 1. 模型1.1. 模型定义type User struct { gorm.Model
- Rex 是 Perl 编写的基于 SSH 链接的集群配置管理系统,语法上类似 Puppet DSL。官网中文版见 http://rex.pe
- 本文实例为大家分享了python实现opencv+scoket网络实时图传的具体代码,供大家参考,具体内容如下服务器分析:1.先通过在服务器
- 学习目标:学会使用windows系统安装MySQL数据库,供大家参考,具体内容如下1.打开浏览器输入SQL官网的下载地址:下载链接2.下载好
- 先了解几个事件对象属性target 指事件源对象,点击嵌套元素最里层的某元素,该元素就是target。IE6/7/8对应的是srcEleme
- 随着网络的发展,越来越多的网络平台应运而生。如何获得更多的流量,吸引更多的眼球已经成为网络平台生存、发展的必要条件。现在网络平台最常见的一种
- 一.问题描述 这是在工作中遇到的一段代码,原理大概和下面类似(判断某一个元素是否符合要求,不符合删除该元素,最后得到符合要求的列表
- 本文实例为大家分享了js实现select二级联动下拉菜单,供大家参考,具体内容如下<%@ page language="ja
- 一、身份验证配置在sqlserver服务端电脑打开SqlServer Managerment Studio管理工具,首先通过Windows身
- 使用cpan安装Net::SSH::Perl:cpan>install Net::SSH::Perl期间遇到了一些问题,记录在此,以备
- Spark GraphX是一个分布式图处理框架,基于 Pregel 接口实现了常用的图算法。包括 PageRank、SVDPlusPlus、