Python sqlite3事務(wù)處理方法實(shí)例分析
本文實(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í), 會自動(dòng)打開一個(gè)事務(wù),
在執(zhí)行 非DML, 非query (非 SELECT 和上面提到的)語句時(shí), 會隱式執(zhí)行commit
可以使用 connection.commit()方法來進(jìn)行提交
注意:
不能和cur.execute("COMMIT")共用
自動(dòng)commit狀態(tài):
生成方式: 在connect()中傳入 isolation_level=None
這樣,在任何DML操作時(shí),都會自動(dòng)提交
事務(wù)處理
connection.execute("BEGIN TRANSACTION")
connection.execute("COMMIT")
如果不使用事務(wù), 批量添加數(shù)據(jù)非常緩慢
數(shù)據(jù)對比:
兩種方式, 事務(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 sys
import time
class 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_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狀態(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)
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python調(diào)用http-post接口的實(shí)現(xiàn)方式
這篇文章主要介紹了Python調(diào)用http-post接口的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Python中往列表中插入字典時(shí),數(shù)據(jù)重復(fù)問題
這篇文章主要介紹了Python中往列表中插入字典時(shí),數(shù)據(jù)重復(fù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Pandas如何將表格的前幾行生成html實(shí)戰(zhàn)案例
這篇文章主要介紹了Pandas如何將表格的前幾行生成html實(shí)戰(zhàn)案例,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
Python完整實(shí)現(xiàn)俄羅斯方塊游戲全解
俄羅斯方塊是一個(gè)最初由阿列克謝帕吉特諾夫在蘇聯(lián)設(shè)計(jì)和編程的益智類視頻游戲。本文將利用python實(shí)現(xiàn)這一經(jīng)典的小游戲,需要的可以參考一下2022-03-03

