侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   245

本文实例讲述了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 sysimport timeclass 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_timeimport 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 * numdef 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实现多并发访问网站功能示例
    下一条:
    Python之str操作方法(详解)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go语言中实现字符串可逆性压缩及解压缩功能(0个评论)
    • 使用go + gin + jwt + qrcode实现网站生成登录二维码在app中扫码登录功能(0个评论)
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客