Python sqlite3事务处理方法实例分析

所属分类: 脚本专栏 / python 阅读数: 626
收藏 0 赞 0 分享

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《Python常见数据库操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

更多精彩内容其他人还在看

Python中模块string.py详解

这篇文章主要介绍了Python中模块之string.py的相关资料,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Python中关键字nonlocal和global的声明与解析

这篇文章主要给大家介绍了关于Python中关键字nonlocal和global的声明与解析的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

python中pandas.DataFrame对行与列求和及添加新行与列示例

pandas是python环境下最有名的数据统计包,而DataFrame翻译为数据框,是一种数据组织方式,这篇文章主要给大家介绍了python中pandas.DataFrame对行与列求和及添加新行与列的方法,文中给出了详细的示例代码,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

Python中str.format()详解

本文主要给大家详细介绍的是python编程中str.format()的基本语法和高级用法,非常的详细,并附有示例,希望大家能够喜欢
收藏 0 赞 0 分享

python中pandas.DataFrame的简单操作方法(创建、索引、增添与删除)

这篇文章主要介绍了python中pandas.DataFrame的简单操作方法,其中包括创建、索引、增添与删除等的相关资料,文中介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

Python IDLE 错误:IDLE''s subprocess didn''t make connection 的解决方案

这篇文章主要介绍了Python IDLE 错误:IDLE's subprocess didn't make connection 的解决方案的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Python中类型检查的详细介绍

Python是一种非常动态的语言,函数定义中完全没有类型约束。下面这篇文章主要给大家详细介绍了Python中类型检查的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用python程序生成word和PDF文档的方法

这篇文章主要给大家介绍了利用python程序生成word和PDF文档的方法,文中给出了详细的介绍和示例代码,相信对大家具有一定的参考价值,有需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

python用装饰器自动注册Tornado路由详解

这篇文章主要给大家介绍了python用装饰器自动注册Tornado路由,文中给出了三个版本的解决方法,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

让python 3支持mysqldb的解决方法

这篇文章主要介绍了关于让python 3支持mysqldb的解决方法,文中给出解决的示例代码,相信对大家具有一定的参考价值,有需要的朋友可以一起来看看。
收藏 0 赞 0 分享
查看更多