日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

pythonsqlite事务_python sqlite3 的事务控制

發(fā)布時間:2025/3/12 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pythonsqlite事务_python sqlite3 的事务控制 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Python sqlite3 的事務(wù)控制

官方文檔的描述:

Controlling Transactions

By default, the?sqlite3?module opens transactions implicitly before a Data Modification Language (DML) statement (i.e.?INSERT/UPDATE/DELETE/REPLACE), and commits transactions implicitly before a non-DML, non-query statement (i. e. anything other than?SELECT?or the aforementioned).

So if you are within a transaction and issue a command like?CREATE?TABLE?...,?VACUUM,?PRAGMA, thesqlite3?module will commit implicitly before executing that command. There are two reasons for doing that. The first is that some of these commands don’t work within transactions. The other reason is that pysqlite needs to keep track of the transaction state (if a transaction is active or not).

You can control which kind of?BEGIN?statements sqlite3 implicitly executes (or none at all) via the?isolation_level?parameter to the?connect()?call, or via the?isolation_level?property of connections.

(譯文:你能控制sqlite3默認執(zhí)行的BEGIN語句類型(或者什么類型都不),這是通過設(shè)置connect()函數(shù)的isolation_level?參數(shù),或connection對象的isolation_level屬性實現(xiàn)的。)

If you want?autocommit mode, then set?isolation_level?to None.

(譯文:如果想使用自動提交模式,設(shè)置isolation_level為None。)

Otherwise leave it at its default, which will result in a plain “BEGIN” statement, or set it to one of SQLite’s supported isolation levels: “DEFERRED”, “IMMEDIATE” or “EXCLUSIVE”.

(譯文:不設(shè)置isolation_level(使用默認)將會執(zhí)行樸素的BEGIN語句(即下文sql語句圖中,BEGIN或BEGINTRANSACTION),或者設(shè)置為“DEFERRED”, “IMMEDIATE” 或“EXCLUSIVE”(即BEGIN DEFERRED/IMMEDIATE/EXCLUSIVETRANSACTION)。)

isolation_level控制的是什么

從上文我們看到,這個變量與BEGIN語句的模式有關(guān),可選值為 “None“,“空“(不設(shè)置),“DEFERRED”, “IMMEDIATE” ,“EXCLUSIVE”

設(shè)置為None即自動提交,即每次寫數(shù)據(jù)庫都提交。

官網(wǎng)文檔前兩段寫的是智能提交,在某些語句自動開啟事務(wù),執(zhí)行某些語句前自動commit。

后邊四個是什么?

注:原文中begin-stmt應(yīng)該是begin-statement的縮寫

這是描述sql語句語法的圖,即:

BEGIN TRANSACTION

BEGIN

BEGIN DEFERRED TRANSACTION

BEGIN DEFERRED

...

情況就明了了,isolation_level決定開啟事務(wù)時使用的是BEGIN TRANSACTION, BEGIN DEFERRED TRANSACTION, BEGIN IMMEDIATE TRANSACTION, BEGIN EXCLUSIVE TRANSACTION中的那種。

我是這么認為的,immediate 是begin語句處獲得PENDING鎖,deferred是獲取RESERVED鎖,update,delete,insert等寫語句出現(xiàn)時才獲得PENDING鎖,exclusive是獲取EXCLUSIVE排他鎖

isolation_level為None是開啟自動commit功能,非None是設(shè)置BEGIN的類型,開啟智能commit。

我理解為:1.BEGIN是自動開啟的 2.設(shè)為None每次寫數(shù)據(jù)庫都會自動commit?3.設(shè)為其他會在某些語句前自動commit,其他地方想立即commit要手動執(zhí)行。

例子來了!

智能commit

import sqlite3

con = sqlite3.connect(":memory:")

cur = con.cursor()

cur.execute("create table people (num, age)")

num = 1

age = 2 * num

while num <= 1000000:

cur.execute("insert into people values (?, ?)", (num, age))

num += 1

age = 2 * num

cur.execute("select count(*) from people")

print cur.fetchone()

保存為test.py執(zhí)行之

# time python test.py

(1000000,)

real ? ?0m6.537s

user ? ?0m6.440s

sys ? ? 0m0.086s

自動commit

import sqlite3

con = sqlite3.connect(":memory:",isolation_level=None)

cur = con.cursor()

cur.execute("create table people (num, age)")

num = 1

age = 2 * num

while num <= 1000000:

cur.execute("insert into people values (?, ?)", (num, age))

num += 1

age = 2 * num

cur.execute("select count(*) from people")

print cur.fetchone()

執(zhí)行之

# time python test.py

(1000000,)

real ? ?0m10.693s

user ? ?0m10.569s

sys ? ? 0m0.099s

智能commit用時6秒,自動commit用時10秒 (例子是寫內(nèi)存,如果寫文件速度會更慢,建議改為寫100條數(shù)據(jù))

智能commit

優(yōu)點:速度快,單進程情況下運行良好

缺點:多個控制流并發(fā)操作數(shù)據(jù)庫時,這邊寫完了,另一邊可能讀不出來

克服缺點:每次寫完數(shù)據(jù),手動執(zhí)行commit

自動commit

優(yōu)點:每次寫數(shù)據(jù)庫都能保證確實寫入了,防止并發(fā)操作數(shù)據(jù)庫時出現(xiàn)邏輯問題

缺點:太慢了!!!

克服缺點:批量操作前手動BEGIN TRANSACTION,操作后手動COMMIC

克服缺點的例子

智能commit時實現(xiàn)即時commit

# coding:utf-8

import sqlite3

con = sqlite3.connect(":memory:")

cur = con.cursor()

cur.execute("create table people (num, age)")

num = 1

age = 2 * num

while num <= 1000000:

cur.execute("insert into people values (?, ?)", (num, age))

con.commit() # 關(guān)鍵在這里

num += 1

age = 2 * num

cur.execute("select count(*) from people")

print cur.fetchone()

time python test.py

(1000000,)

real ? ?0m20.797s

user ? ?0m20.611s

sys ? ? 0m0.156s

自動commit時阻止即時commit

# coding:utf-8

import sqlite3

con = sqlite3.connect(":memory:",isolation_level=None)

cur = con.cursor()

cur.execute("create table people (num, age)")

num = 1

age = 2 * num

cur.execute("BEGIN TRANSACTION") # 關(guān)鍵點

while num <= 1000000:

cur.execute("insert into people values (?, ?)", (num, age))

num += 1

age = 2 * num

cur.execute("COMMIT") #關(guān)鍵點

cur.execute("select count(*) from people")

print cur.fetchone()

# time python test.py

(1000000,)

real ? ?0m6.649s

user ? ?0m6.555s

sys ? ? 0m0.076s

這次,智能commit用時20秒(性能下降很多),自動commit用時6秒?,完全反過來了

總結(jié)

以上是生活随笔為你收集整理的pythonsqlite事务_python sqlite3 的事务控制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。