国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python sqlite3事務(wù)處理方法實(shí)例分析

2020-02-16 01:43:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Python sqlite3事務(wù)處理方法。分享給大家供大家參考,具體如下:

sqlite3事務(wù)總結(jié):

在connect()中不傳入 isolation_level

事務(wù)處理:

使用connection.commit()

#!/usr/bin/env python# -*- coding:utf-8 -*-'''sqlite3事務(wù)總結(jié):在connect()中不傳入 isolation_level事務(wù)處理:  使用connection.commit()分析:  智能commit狀態(tài):    生成方式: 在connect()中不傳入 isolation_level, 此時(shí)isolation_level==''      在進(jìn)行 執(zhí)行Data Modification Language (DML) 操作(INSERT/UPDATE/DELETE/REPLACE)時(shí), 會(huì)自動(dòng)打開(kāi)一個(gè)事務(wù),      在執(zhí)行 非DML, 非query (非 SELECT 和上面提到的)語(yǔ)句時(shí), 會(huì)隱式執(zhí)行commit      可以使用 connection.commit()方法來(lái)進(jìn)行提交    注意:      不能和cur.execute("COMMIT")共用  自動(dòng)commit狀態(tài):    生成方式: 在connect()中傳入 isolation_level=None      這樣,在任何DML操作時(shí),都會(huì)自動(dòng)提交    事務(wù)處理      connection.execute("BEGIN TRANSACTION")      connection.execute("COMMIT")    如果不使用事務(wù), 批量添加數(shù)據(jù)非常緩慢數(shù)據(jù)對(duì)比:  兩種方式, 事務(wù)耗時(shí)差別不大  count = 100000    智能commit即時(shí)提交耗時(shí): 0.621    自動(dòng)commit耗時(shí): 0.601    智能commit即時(shí)提交耗時(shí): 0.588    自動(dòng)commit耗時(shí): 0.581    智能commit即時(shí)提交耗時(shí): 0.598    自動(dòng)commit耗時(shí): 0.588    智能commit即時(shí)提交耗時(shí): 0.589    自動(dòng)commit耗時(shí): 0.602    智能commit即時(shí)提交耗時(shí): 0.588    自動(dòng)commit耗時(shí): 0.622'''import sysimport timeclass Elapse_time(object):  '''耗時(shí)統(tǒng)計(jì)工具'''  def __init__(self, prompt=''):    self.prompt = prompt    self.start = time.time()  def __del__(self):    print('%s耗時(shí): %.3f' % (self.prompt, time.time() - self.start))CElapseTime = Elapse_timeimport sqlite3# -------------------------------------------------------------------------------# 測(cè)試#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狀態(tài)下, 不能和cur.execute("COMMIT")共用  '''  connection, cursor = prepare()  elapse_time = Elapse_time(' 智能commit')  db_insert_values(cursor, count)  #cursor.execute("COMMIT") #產(chǎn)生異常  cursor.execute("select count(*) from people")  print (cursor.fetchone())def study_case2_autocommit(count):  connection, cursor = prepare(isolation_level = None)  elapse_time = Elapse_time(' 自動(dòng)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即時(shí)提交')  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(' 自動(dòng)commit')  connection.execute("BEGIN TRANSACTION;") # 關(guān)鍵點(diǎn)  db_insert_values(cursor, count)  connection.execute("COMMIT;") #關(guān)鍵點(diǎn)  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) #不提交數(shù)據(jù)    #study_case2_autocommit(count) #非常緩慢    study_case3_intelligent_commit_manual(count)    study_case4_autocommit_transaction(count)            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 滕州市| 南召县| 扎囊县| 抚远县| 广饶县| 上虞市| 延津县| 花莲县| 乌拉特中旗| 来安县| 汉沽区| 宜章县| 乌兰县| 建阳市| 合川市| 嵊州市| 博罗县| 凤冈县| 洛宁县| 高州市| 搜索| 辽阳县| 太湖县| 大埔区| 武强县| 阿坝| 晴隆县| 新干县| 苏尼特左旗| 阿巴嘎旗| 怀仁县| 商河县| 永寿县| 永兴县| 安塞县| 白玉县| 科技| 宽城| 凤庆县| 泾阳县| 嘉祥县|