Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d7d6cf

Browse files
committed
Same documentation for sqlite3 module as in 2.6.
1 parent e7ea745 commit 0d7d6cf

2 files changed

Lines changed: 66 additions & 11 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: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ A :class:`Connection` instance has the following attributes and methods:
230230
:class:`sqlite3.Cursor`.
231231

232232

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

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

332350

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

335366
You can change this attribute to a callable that accepts the cursor and the
@@ -452,29 +483,29 @@ A :class:`Cursor` instance has the following attributes and methods:
452483
.. literalinclude:: ../includes/sqlite3/executescript.py
453484

454485

455-
.. method:: Cursor.fetchone()
456-
486+
.. method:: Cursor.fetchone()
487+
457488
Fetches the next row of a query result set, returning a single sequence,
458489
or ``None`` when no more data is available.
459490

460491

461492
.. method:: Cursor.fetchmany([size=cursor.arraysize])
462-
493+
463494
Fetches the next set of rows of a query result, returning a list. An empty
464495
list is returned when no more rows are available.
465-
496+
466497
The number of rows to fetch per call is specified by the *size* parameter.
467498
If it is not given, the cursor's arraysize determines the number of rows
468499
to be fetched. The method should try to fetch as many rows as indicated by
469500
the size parameter. If this is not possible due to the specified number of
470501
rows not being available, fewer rows may be returned.
471-
502+
472503
Note there are performance considerations involved with the *size* parameter.
473504
For optimal performance, it is usually best to use the arraysize attribute.
474505
If the *size* parameter is used, then it is best for it to retain the same
475506
value from one :meth:`fetchmany` call to the next.
476-
477-
.. method:: Cursor.fetchall()
507+
508+
.. method:: Cursor.fetchall()
478509

479510
Fetches all (remaining) rows of a query result, returning a list. Note that
480511
the cursor's arraysize attribute can affect the performance of this operation.
@@ -692,10 +723,6 @@ Otherwise leave it at its default, which will result in a plain "BEGIN"
692723
statement, or set it to one of SQLite's supported isolation levels: DEFERRED,
693724
IMMEDIATE or EXCLUSIVE.
694725

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

700727

701728
Using pysqlite efficiently
@@ -727,3 +754,15 @@ case-insensitively by name:
727754

728755
.. literalinclude:: ../includes/sqlite3/rowclass.py
729756

757+
758+
Using the connection as a context manager
759+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
760+
761+
.. versionadded:: 2.6
762+
763+
Connection objects can be used as context managers
764+
that automatically commit or rollback transactions. In the event of an
765+
exception, the transaction is rolled back; otherwise, the transaction is
766+
committed:
767+
768+
.. literalinclude:: ../includes/sqlite3/ctx_manager.py

0 commit comments

Comments
 (0)