@@ -557,103 +557,6 @@ Connection objects
557
557
558
558
An SQLite database connection has the following attributes and methods:
559
559
560
- .. attribute :: isolation_level
561
-
562
- This attribute controls the :ref: `transaction handling
563
- <sqlite3-controlling-transactions>` performed by :mod: `!sqlite3 `.
564
- If set to ``None ``, transactions are never implicitly opened.
565
- If set to one of ``"DEFERRED" ``, ``"IMMEDIATE" ``, or ``"EXCLUSIVE" ``,
566
- corresponding to the underlying `SQLite transaction behaviour `_,
567
- implicit :ref: `transaction management
568
- <sqlite3-controlling-transactions>` is performed.
569
-
570
- If not overridden by the *isolation_level * parameter of :func: `connect `,
571
- the default is ``"" ``, which is an alias for ``"DEFERRED" ``.
572
-
573
- .. attribute :: in_transaction
574
-
575
- This read-only attribute corresponds to the low-level SQLite
576
- `autocommit mode `_.
577
-
578
- ``True `` if a transaction is active (there are uncommitted changes),
579
- ``False `` otherwise.
580
-
581
- .. versionadded :: 3.2
582
-
583
- .. attribute :: row_factory
584
-
585
- A callable that accepts two arguments,
586
- a :class: `Cursor ` object and the raw row results as a :class: `tuple `,
587
- and returns a custom object representing an SQLite row.
588
-
589
- Example:
590
-
591
- .. doctest ::
592
-
593
- >>> def dict_factory (cursor , row ):
594
- ... col_names = [col[0 ] for col in cursor.description]
595
- ... return {key: value for key, value in zip (col_names, row)}
596
- >>> con = sqlite3.connect(" :memory:" )
597
- >>> con.row_factory = dict_factory
598
- >>> for row in con.execute(" SELECT 1 AS a, 2 AS b" ):
599
- ... print (row)
600
- {'a': 1, 'b': 2}
601
-
602
- If returning a tuple doesn't suffice and you want name-based access to
603
- columns, you should consider setting :attr: `row_factory ` to the
604
- highly optimized :class: `sqlite3.Row ` type. :class: `Row ` provides both
605
- index-based and case-insensitive name-based access to columns with almost no
606
- memory overhead. It will probably be better than your own custom
607
- dictionary-based approach or even a db_row based solution.
608
-
609
- .. XXX what's a db_row-based solution?
610
-
611
- .. attribute :: text_factory
612
-
613
- A callable that accepts a :class: `bytes ` parameter and returns a text
614
- representation of it.
615
- The callable is invoked for SQLite values with the ``TEXT `` data type.
616
- By default, this attribute is set to :class: `str `.
617
- If you want to return ``bytes `` instead, set *text_factory * to ``bytes ``.
618
-
619
- Example:
620
-
621
- .. testcode ::
622
-
623
- con = sqlite3.connect(":memory: ")
624
- cur = con.cursor()
625
-
626
- AUSTRIA = "Österreich"
627
-
628
- # by default, rows are returned as str
629
- cur.execute("SELECT ?", (AUSTRIA,))
630
- row = cur.fetchone()
631
- assert row[0] == AUSTRIA
632
-
633
- # but we can make sqlite3 always return bytestrings ...
634
- con.text_factory = bytes
635
- cur.execute("SELECT ?", (AUSTRIA,))
636
- row = cur.fetchone()
637
- assert type(row[0]) is bytes
638
- # the bytestrings will be encoded in UTF-8, unless you stored garbage in the
639
- # database ...
640
- assert row[0] == AUSTRIA.encode("utf-8")
641
-
642
- # we can also implement a custom text_factory ...
643
- # here we implement one that appends "foo" to all strings
644
- con.text_factory = lambda x: x.decode("utf-8") + "foo"
645
- cur.execute("SELECT ?", ("bar",))
646
- row = cur.fetchone()
647
- assert row[0] == "barfoo"
648
-
649
- con.close()
650
-
651
- .. attribute :: total_changes
652
-
653
- Return the total number of database rows that have been modified, inserted, or
654
- deleted since the database connection was opened.
655
-
656
-
657
560
.. method :: cursor(factory=Cursor)
658
561
659
562
Create and return a :class: `Cursor ` object.
@@ -1307,6 +1210,102 @@ Connection objects
1307
1210
1308
1211
.. versionadded :: 3.11
1309
1212
1213
+ .. attribute :: in_transaction
1214
+
1215
+ This read-only attribute corresponds to the low-level SQLite
1216
+ `autocommit mode `_.
1217
+
1218
+ ``True `` if a transaction is active (there are uncommitted changes),
1219
+ ``False `` otherwise.
1220
+
1221
+ .. versionadded :: 3.2
1222
+
1223
+ .. attribute :: isolation_level
1224
+
1225
+ This attribute controls the :ref: `transaction handling
1226
+ <sqlite3-controlling-transactions>` performed by :mod: `!sqlite3 `.
1227
+ If set to ``None ``, transactions are never implicitly opened.
1228
+ If set to one of ``"DEFERRED" ``, ``"IMMEDIATE" ``, or ``"EXCLUSIVE" ``,
1229
+ corresponding to the underlying `SQLite transaction behaviour `_,
1230
+ implicit :ref: `transaction management
1231
+ <sqlite3-controlling-transactions>` is performed.
1232
+
1233
+ If not overridden by the *isolation_level * parameter of :func: `connect `,
1234
+ the default is ``"" ``, which is an alias for ``"DEFERRED" ``.
1235
+
1236
+ .. attribute :: row_factory
1237
+
1238
+ A callable that accepts two arguments,
1239
+ a :class: `Cursor ` object and the raw row results as a :class: `tuple `,
1240
+ and returns a custom object representing an SQLite row.
1241
+
1242
+ Example:
1243
+
1244
+ .. doctest ::
1245
+
1246
+ >>> def dict_factory (cursor , row ):
1247
+ ... col_names = [col[0 ] for col in cursor.description]
1248
+ ... return {key: value for key, value in zip (col_names, row)}
1249
+ >>> con = sqlite3.connect(" :memory:" )
1250
+ >>> con.row_factory = dict_factory
1251
+ >>> for row in con.execute(" SELECT 1 AS a, 2 AS b" ):
1252
+ ... print (row)
1253
+ {'a': 1, 'b': 2}
1254
+
1255
+ If returning a tuple doesn't suffice and you want name-based access to
1256
+ columns, you should consider setting :attr: `row_factory ` to the
1257
+ highly optimized :class: `sqlite3.Row ` type. :class: `Row ` provides both
1258
+ index-based and case-insensitive name-based access to columns with almost no
1259
+ memory overhead. It will probably be better than your own custom
1260
+ dictionary-based approach or even a db_row based solution.
1261
+
1262
+ .. XXX what's a db_row-based solution?
1263
+
1264
+ .. attribute :: text_factory
1265
+
1266
+ A callable that accepts a :class: `bytes ` parameter and returns a text
1267
+ representation of it.
1268
+ The callable is invoked for SQLite values with the ``TEXT `` data type.
1269
+ By default, this attribute is set to :class: `str `.
1270
+ If you want to return ``bytes `` instead, set *text_factory * to ``bytes ``.
1271
+
1272
+ Example:
1273
+
1274
+ .. testcode ::
1275
+
1276
+ con = sqlite3.connect(":memory: ")
1277
+ cur = con.cursor()
1278
+
1279
+ AUSTRIA = "Österreich"
1280
+
1281
+ # by default, rows are returned as str
1282
+ cur.execute("SELECT ?", (AUSTRIA,))
1283
+ row = cur.fetchone()
1284
+ assert row[0] == AUSTRIA
1285
+
1286
+ # but we can make sqlite3 always return bytestrings ...
1287
+ con.text_factory = bytes
1288
+ cur.execute("SELECT ?", (AUSTRIA,))
1289
+ row = cur.fetchone()
1290
+ assert type(row[0]) is bytes
1291
+ # the bytestrings will be encoded in UTF-8, unless you stored garbage in the
1292
+ # database ...
1293
+ assert row[0] == AUSTRIA.encode("utf-8")
1294
+
1295
+ # we can also implement a custom text_factory ...
1296
+ # here we implement one that appends "foo" to all strings
1297
+ con.text_factory = lambda x: x.decode("utf-8") + "foo"
1298
+ cur.execute("SELECT ?", ("bar",))
1299
+ row = cur.fetchone()
1300
+ assert row[0] == "barfoo"
1301
+
1302
+ con.close()
1303
+
1304
+ .. attribute :: total_changes
1305
+
1306
+ Return the total number of database rows that have been modified, inserted, or
1307
+ deleted since the database connection was opened.
1308
+
1310
1309
1311
1310
.. _sqlite3-cursor-objects :
1312
1311
0 commit comments