Python sqlite3事務(wù)處理方法實例分析
本文實例講述了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, 此時isolation_level=='' 在進行 執(zhí)行Data Modification Language (DML) 操作(INSERT/UPDATE/DELETE/REPLACE)時, 會自動打開一個事務(wù), 在執(zhí)行 非DML, 非query (非 SELECT 和上面提到的)語句時, 會隱式執(zhí)行commit 可以使用 connection.commit()方法來進行提交 注意: 不能和cur.execute("COMMIT")共用 自動commit狀態(tài): 生成方式: 在connect()中傳入 isolation_level=None 這樣,在任何DML操作時,都會自動提交 事務(wù)處理 connection.execute("BEGIN TRANSACTION") connection.execute("COMMIT") 如果不使用事務(wù), 批量添加數(shù)據(jù)非常緩慢 數(shù)據(jù)對比: 兩種方式, 事務(wù)耗時差別不大 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): '''耗時統(tǒng)計工具''' 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狀態(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(' 自動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;") # 關(guān)鍵點 db_insert_values(cursor, count) connection.execute("COMMIT;") #關(guā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īng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python調(diào)用http-post接口的實現(xiàn)方式
這篇文章主要介紹了Python調(diào)用http-post接口的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08Python中往列表中插入字典時,數(shù)據(jù)重復(fù)問題
這篇文章主要介紹了Python中往列表中插入字典時,數(shù)據(jù)重復(fù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02Pandas如何將表格的前幾行生成html實戰(zhàn)案例
這篇文章主要介紹了Pandas如何將表格的前幾行生成html實戰(zhàn)案例,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08