Python の sqlite3 ライブラリのトランザクション制御に関する項目が未翻訳でしたので、英語が苦手なりに頑張って読みました。せっかく作ったメモですので稚拙な内容ながら投稿させていただきます。
本文
By default, the
sqlite3module 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 thanSELECTor the aforementioned).
既定では sqlite3 は DML文の前に暗黙的にトランザクションを発行します。また、非DML文 1 や非クエリ文の前には暗黙的にトランザクションをコミットします。
語句
implicitly = [副] 暗黙的に
anything other than ~ = ~以外の
aforementioned = [形] 前述の
So if you are within a transaction and issue a command like
CREATE TABLE ...,VACUUM,PRAGMA, thesqlite3module 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 sqlite3 needs to keep track of the transaction state (if a transaction is active or not). The current transaction state is exposed through theConnection.in_transactionattribute of the connection object.
トランザクション中に CREATE TABLE ... などの命令が呼び出されたときは、その文を実行する前に暗黙的にコミットします。この理由は2つあります。
- それらの命令はトランザクション内で実行できないから。
- sqlite3 は トランザクションの状態(アクティブであるかどうか)を追跡する必要があるから。
トランザクションの状態は Connection.in_transaction で取得できます。
語句
issue = [他動] ~を発布する,~を発行する
keep track of... = …を追跡する
expose = [他動] ~をさらす
You can control which kind of
BEGINstatementssqlite3implicitly executes (or none at all) via theisolation_levelparameter to theconnect()call, or via theisolation_levelproperty of connections.
sqlite3 が暗黙的に実行する BEGIN 文の種類は sqlite3.connect の 引数 isolation_level において設定可能です。インスタンス化の時点のみでなく プロパティ isolation_level を通しても随時設定可能です。
If you want autocommit mode, then set
isolation_leveltoNone.
前述の isolation に None を設定すると autocommit モードになります。
Otherwise leave it at its default, which will result in a plain
BEGINstatement, or set it to one of SQLite's supported isolation levels:"DEFERRED","IMMEDIATE"or"EXCLUSIVE".
isolation_level に "DEFERRED", "IMMEDIATE", "EXCLUSIVE" が設定された場合はそれぞれに対応した隔離レベルが適用されます。デフォルトのままであれば、通常の BEGIN が実行されます。
語句
otherwise = [副] さもなくば,もしそうでなければ
result in ... = 〔結果的に〕~をもたらす,~につながる