Skip to content

Commit 4130930

Browse files
committed
Brought documentation for sqlite3 module up-to-date. Fixed Issue1625205 which
complained about commit, rollback and close not being documented.
1 parent 554d4f0 commit 4130930

2 files changed

Lines changed: 59 additions & 5 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sqlite3
2+
3+
con = sqlite3.connect(":memory:")
4+
con.execute("create table person (id integer primary key, firstname varchar unique)")
5+
6+
# Successful, con.commit() is called automatically afterwards
7+
with con:
8+
con.execute("insert into person(firstname) values (?)", ("Joe",))
9+
10+
# con.rollback() is called after the with block finishes with an exception, the
11+
# exception is still raised and must be catched
12+
try:
13+
with con:
14+
con.execute("insert into person(firstname) values (?)", ("Joe",))
15+
except sqlite3.IntegrityError:
16+
print "couldn't add Joe twice"

Doc/library/sqlite3.rst

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,24 @@ A :class:`Connection` instance has the following attributes and methods:
232232
:class:`sqlite3.Cursor`.
233233

234234

235+
.. method:: Connection.commit()
236+
237+
This method commits the current transaction. If you don't call this method,
238+
anything you did since the last call to commit() is not visible from from
239+
other database connections. If you wonder why you don't see the data you've
240+
written to the database, please check you didn't forget to call this method.
241+
242+
.. method:: Connection.rollback()
243+
244+
This method rolls back any changes to the database since the last call to
245+
:meth:`commit`.
246+
247+
.. method:: Connection.close()
248+
249+
This closes the database connection. Note that this does not automatically
250+
call :meth:`commit`. If you just close your database connection without
251+
calling :meth:`commit` first, your changes will be lost!
252+
235253
.. method:: Connection.execute(sql, [parameters])
236254

237255
This is a nonstandard shortcut that creates an intermediate cursor object by
@@ -245,7 +263,6 @@ A :class:`Connection` instance has the following attributes and methods:
245263
calling the cursor method, then calls the cursor's :meth:`executemany` method
246264
with the parameters given.
247265

248-
249266
.. method:: Connection.executescript(sql_script)
250267

251268
This is a nonstandard shortcut that creates an intermediate cursor object by
@@ -332,6 +349,19 @@ A :class:`Connection` instance has the following attributes and methods:
332349
one. All necessary constants are available in the :mod:`sqlite3` module.
333350

334351

352+
.. method:: Connection.set_progress_handler(handler, n)
353+
354+
.. versionadded:: 2.6
355+
356+
This routine registers a callback. The callback is invoked for every *n*
357+
instructions of the SQLite virtual machine. This is useful if you want to
358+
get called from SQLite during long-running operations, for example to update
359+
a GUI.
360+
361+
If you want to clear any previously installed progress handler, call the
362+
method with :const:`None` for *handler*.
363+
364+
335365
.. attribute:: Connection.row_factory
336366

337367
You can change this attribute to a callable that accepts the cursor and the
@@ -701,10 +731,6 @@ Otherwise leave it at its default, which will result in a plain "BEGIN"
701731
statement, or set it to one of SQLite's supported isolation levels: DEFERRED,
702732
IMMEDIATE or EXCLUSIVE.
703733

704-
As the :mod:`sqlite3` module needs to keep track of the transaction state, you
705-
should not use ``OR ROLLBACK`` or ``ON CONFLICT ROLLBACK`` in your SQL. Instead,
706-
catch the :exc:`IntegrityError` and call the :meth:`rollback` method of the
707-
connection yourself.
708734

709735

710736
Using pysqlite efficiently
@@ -736,3 +762,15 @@ case-insensitively by name:
736762

737763
.. literalinclude:: ../includes/sqlite3/rowclass.py
738764

765+
766+
Using the connection as a context manager
767+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
768+
769+
.. versionadded:: 2.6
770+
771+
Connection objects can be used as context managers
772+
that automatically commit or rollback transactions. In the event of an
773+
exception, the transaction is rolled back; otherwise, the transaction is
774+
committed:
775+
776+
.. literalinclude:: ../includes/sqlite3/ctx_manager.py

0 commit comments

Comments
 (0)