On Friday, 25 October, 2019 10:44, Peng Yu <pengyu...@gmail.com> wrote:

>The python manual just tell me what I should do but it is not very
>clear what commit() actually does under the hood.

>https://docs.python.org/2/library/sqlite3.html

>"""
>commit()

>    This method commits the current transaction. If you don’t call
>this method, anything you did since the last call to commit() is not
>visible from other database connections. If you wonder why you don’t
>see the data you’ve written to the database, please check you didn’t
>forget to call this method.
>"""

commit() returns the database to autocommit mode ending the transaction that 
was commenced with a begin command (it issues a COMMIT to the underlying 
sqlite3 database).  If the wrapper is operating in "magical mode" then 
statements which might update the database might be preceded with a magical 
begin statement (with a probability somewhere between 0% and 100% of the time). 
 See the isolation_level parameter when opening a connection for a description 
of the magical incantation modes available.  The text you quoted above assumes 
that magical mode is in effect.

By default the 'DEFERRED' magic mode is in effect.  You can turn off magical 
mode by using isolation_level=None when opening the connection, in which case 
you must BEGIN and COMMIT transactions yourself (the magical faery dust is 
turned off).  When the magical mode is turned off, each statement is executed 
in its own transaction in autocommit mode, or inside an explicit transaction 
which you BEGIN and COMMIT/ROLLBACK.

>So, only if I want to write something to the db, I need to call
>commit()? If I just read something from the db, there is no need to
>call commit()?

If you write to the database, and magical mode is enabled (isolation_level is 
any value other than None), and the wrapper faery managed to figure out that 
you were writing to the database and therefore issued a BEGIN, then you must 
commit the transaction in order to end the transaction that the magical 
transaction faery started for you.

Also, if you commenced a transaction with an explicit BEGIN command, then you 
must COMMIT that transaction in order for it to be visible to anyone else.

When a transaction is in process outstanding transactions are rolled back when 
the connection is closed.

-- 
The fact that there's a Highway to Hell but only a Stairway to Heaven says a 
lot about anticipated traffic volume. 



_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to