Skip to content

Commit 49cceeb

Browse files
gh-96702: Order methods before attrs in sqlite3.Connection docs (#96703)
1 parent 12c5f32 commit 49cceeb

File tree

1 file changed

+96
-97
lines changed

1 file changed

+96
-97
lines changed

Doc/library/sqlite3.rst

Lines changed: 96 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -570,103 +570,6 @@ Connection objects
570570

571571
An SQLite database connection has the following attributes and methods:
572572

573-
.. attribute:: isolation_level
574-
575-
This attribute controls the :ref:`transaction handling
576-
<sqlite3-controlling-transactions>` performed by :mod:`!sqlite3`.
577-
If set to ``None``, transactions are never implicitly opened.
578-
If set to one of ``"DEFERRED"``, ``"IMMEDIATE"``, or ``"EXCLUSIVE"``,
579-
corresponding to the underlying `SQLite transaction behaviour`_,
580-
implicit :ref:`transaction management
581-
<sqlite3-controlling-transactions>` is performed.
582-
583-
If not overridden by the *isolation_level* parameter of :func:`connect`,
584-
the default is ``""``, which is an alias for ``"DEFERRED"``.
585-
586-
.. attribute:: in_transaction
587-
588-
This read-only attribute corresponds to the low-level SQLite
589-
`autocommit mode`_.
590-
591-
``True`` if a transaction is active (there are uncommitted changes),
592-
``False`` otherwise.
593-
594-
.. versionadded:: 3.2
595-
596-
.. attribute:: row_factory
597-
598-
A callable that accepts two arguments,
599-
a :class:`Cursor` object and the raw row results as a :class:`tuple`,
600-
and returns a custom object representing an SQLite row.
601-
602-
Example:
603-
604-
.. doctest::
605-
606-
>>> def dict_factory(cursor, row):
607-
... col_names = [col[0] for col in cursor.description]
608-
... return {key: value for key, value in zip(col_names, row)}
609-
>>> con = sqlite3.connect(":memory:")
610-
>>> con.row_factory = dict_factory
611-
>>> for row in con.execute("SELECT 1 AS a, 2 AS b"):
612-
... print(row)
613-
{'a': 1, 'b': 2}
614-
615-
If returning a tuple doesn't suffice and you want name-based access to
616-
columns, you should consider setting :attr:`row_factory` to the
617-
highly optimized :class:`sqlite3.Row` type. :class:`Row` provides both
618-
index-based and case-insensitive name-based access to columns with almost no
619-
memory overhead. It will probably be better than your own custom
620-
dictionary-based approach or even a db_row based solution.
621-
622-
.. XXX what's a db_row-based solution?
623-
624-
.. attribute:: text_factory
625-
626-
A callable that accepts a :class:`bytes` parameter and returns a text
627-
representation of it.
628-
The callable is invoked for SQLite values with the ``TEXT`` data type.
629-
By default, this attribute is set to :class:`str`.
630-
If you want to return ``bytes`` instead, set *text_factory* to ``bytes``.
631-
632-
Example:
633-
634-
.. testcode::
635-
636-
con = sqlite3.connect(":memory:")
637-
cur = con.cursor()
638-
639-
AUSTRIA = "Österreich"
640-
641-
# by default, rows are returned as str
642-
cur.execute("SELECT ?", (AUSTRIA,))
643-
row = cur.fetchone()
644-
assert row[0] == AUSTRIA
645-
646-
# but we can make sqlite3 always return bytestrings ...
647-
con.text_factory = bytes
648-
cur.execute("SELECT ?", (AUSTRIA,))
649-
row = cur.fetchone()
650-
assert type(row[0]) is bytes
651-
# the bytestrings will be encoded in UTF-8, unless you stored garbage in the
652-
# database ...
653-
assert row[0] == AUSTRIA.encode("utf-8")
654-
655-
# we can also implement a custom text_factory ...
656-
# here we implement one that appends "foo" to all strings
657-
con.text_factory = lambda x: x.decode("utf-8") + "foo"
658-
cur.execute("SELECT ?", ("bar",))
659-
row = cur.fetchone()
660-
assert row[0] == "barfoo"
661-
662-
con.close()
663-
664-
.. attribute:: total_changes
665-
666-
Return the total number of database rows that have been modified, inserted, or
667-
deleted since the database connection was opened.
668-
669-
670573
.. method:: cursor(factory=Cursor)
671574

672575
Create and return a :class:`Cursor` object.
@@ -1320,6 +1223,102 @@ Connection objects
13201223

13211224
.. versionadded:: 3.11
13221225

1226+
.. attribute:: in_transaction
1227+
1228+
This read-only attribute corresponds to the low-level SQLite
1229+
`autocommit mode`_.
1230+
1231+
``True`` if a transaction is active (there are uncommitted changes),
1232+
``False`` otherwise.
1233+
1234+
.. versionadded:: 3.2
1235+
1236+
.. attribute:: isolation_level
1237+
1238+
This attribute controls the :ref:`transaction handling
1239+
<sqlite3-controlling-transactions>` performed by :mod:`!sqlite3`.
1240+
If set to ``None``, transactions are never implicitly opened.
1241+
If set to one of ``"DEFERRED"``, ``"IMMEDIATE"``, or ``"EXCLUSIVE"``,
1242+
corresponding to the underlying `SQLite transaction behaviour`_,
1243+
implicit :ref:`transaction management
1244+
<sqlite3-controlling-transactions>` is performed.
1245+
1246+
If not overridden by the *isolation_level* parameter of :func:`connect`,
1247+
the default is ``""``, which is an alias for ``"DEFERRED"``.
1248+
1249+
.. attribute:: row_factory
1250+
1251+
A callable that accepts two arguments,
1252+
a :class:`Cursor` object and the raw row results as a :class:`tuple`,
1253+
and returns a custom object representing an SQLite row.
1254+
1255+
Example:
1256+
1257+
.. doctest::
1258+
1259+
>>> def dict_factory(cursor, row):
1260+
... col_names = [col[0] for col in cursor.description]
1261+
... return {key: value for key, value in zip(col_names, row)}
1262+
>>> con = sqlite3.connect(":memory:")
1263+
>>> con.row_factory = dict_factory
1264+
>>> for row in con.execute("SELECT 1 AS a, 2 AS b"):
1265+
... print(row)
1266+
{'a': 1, 'b': 2}
1267+
1268+
If returning a tuple doesn't suffice and you want name-based access to
1269+
columns, you should consider setting :attr:`row_factory` to the
1270+
highly optimized :class:`sqlite3.Row` type. :class:`Row` provides both
1271+
index-based and case-insensitive name-based access to columns with almost no
1272+
memory overhead. It will probably be better than your own custom
1273+
dictionary-based approach or even a db_row based solution.
1274+
1275+
.. XXX what's a db_row-based solution?
1276+
1277+
.. attribute:: text_factory
1278+
1279+
A callable that accepts a :class:`bytes` parameter and returns a text
1280+
representation of it.
1281+
The callable is invoked for SQLite values with the ``TEXT`` data type.
1282+
By default, this attribute is set to :class:`str`.
1283+
If you want to return ``bytes`` instead, set *text_factory* to ``bytes``.
1284+
1285+
Example:
1286+
1287+
.. testcode::
1288+
1289+
con = sqlite3.connect(":memory:")
1290+
cur = con.cursor()
1291+
1292+
AUSTRIA = "Österreich"
1293+
1294+
# by default, rows are returned as str
1295+
cur.execute("SELECT ?", (AUSTRIA,))
1296+
row = cur.fetchone()
1297+
assert row[0] == AUSTRIA
1298+
1299+
# but we can make sqlite3 always return bytestrings ...
1300+
con.text_factory = bytes
1301+
cur.execute("SELECT ?", (AUSTRIA,))
1302+
row = cur.fetchone()
1303+
assert type(row[0]) is bytes
1304+
# the bytestrings will be encoded in UTF-8, unless you stored garbage in the
1305+
# database ...
1306+
assert row[0] == AUSTRIA.encode("utf-8")
1307+
1308+
# we can also implement a custom text_factory ...
1309+
# here we implement one that appends "foo" to all strings
1310+
con.text_factory = lambda x: x.decode("utf-8") + "foo"
1311+
cur.execute("SELECT ?", ("bar",))
1312+
row = cur.fetchone()
1313+
assert row[0] == "barfoo"
1314+
1315+
con.close()
1316+
1317+
.. attribute:: total_changes
1318+
1319+
Return the total number of database rows that have been modified, inserted, or
1320+
deleted since the database connection was opened.
1321+
13231322

13241323
.. _sqlite3-cursor-objects:
13251324

0 commit comments

Comments
 (0)