@@ -515,103 +515,6 @@ Connection objects
515
515
516
516
An SQLite database connection has the following attributes and methods:
517
517
518
- .. attribute :: isolation_level
519
-
520
- This attribute controls the :ref: `transaction handling
521
- <sqlite3-controlling-transactions>` performed by :mod: `!sqlite3 `.
522
- If set to ``None ``, transactions are never implicitly opened.
523
- If set to one of ``"DEFERRED" ``, ``"IMMEDIATE" ``, or ``"EXCLUSIVE" ``,
524
- corresponding to the underlying `SQLite transaction behaviour `_,
525
- implicit :ref: `transaction management
526
- <sqlite3-controlling-transactions>` is performed.
527
-
528
- If not overridden by the *isolation_level * parameter of :func: `connect `,
529
- the default is ``"" ``, which is an alias for ``"DEFERRED" ``.
530
-
531
- .. attribute :: in_transaction
532
-
533
- This read-only attribute corresponds to the low-level SQLite
534
- `autocommit mode `_.
535
-
536
- ``True `` if a transaction is active (there are uncommitted changes),
537
- ``False `` otherwise.
538
-
539
- .. versionadded :: 3.2
540
-
541
- .. attribute :: row_factory
542
-
543
- A callable that accepts two arguments,
544
- a :class: `Cursor ` object and the raw row results as a :class: `tuple `,
545
- and returns a custom object representing an SQLite row.
546
-
547
- Example:
548
-
549
- .. doctest ::
550
-
551
- >>> def dict_factory (cursor , row ):
552
- ... col_names = [col[0 ] for col in cursor.description]
553
- ... return {key: value for key, value in zip (col_names, row)}
554
- >>> con = sqlite3.connect(" :memory:" )
555
- >>> con.row_factory = dict_factory
556
- >>> for row in con.execute(" SELECT 1 AS a, 2 AS b" ):
557
- ... print (row)
558
- {'a': 1, 'b': 2}
559
-
560
- If returning a tuple doesn't suffice and you want name-based access to
561
- columns, you should consider setting :attr: `row_factory ` to the
562
- highly optimized :class: `sqlite3.Row ` type. :class: `Row ` provides both
563
- index-based and case-insensitive name-based access to columns with almost no
564
- memory overhead. It will probably be better than your own custom
565
- dictionary-based approach or even a db_row based solution.
566
-
567
- .. XXX what's a db_row-based solution?
568
-
569
- .. attribute :: text_factory
570
-
571
- A callable that accepts a :class: `bytes ` parameter and returns a text
572
- representation of it.
573
- The callable is invoked for SQLite values with the ``TEXT `` data type.
574
- By default, this attribute is set to :class: `str `.
575
- If you want to return ``bytes `` instead, set *text_factory * to ``bytes ``.
576
-
577
- Example:
578
-
579
- .. testcode ::
580
-
581
- con = sqlite3.connect(":memory: ")
582
- cur = con.cursor()
583
-
584
- AUSTRIA = "Österreich"
585
-
586
- # by default, rows are returned as str
587
- cur.execute("SELECT ?", (AUSTRIA,))
588
- row = cur.fetchone()
589
- assert row[0] == AUSTRIA
590
-
591
- # but we can make sqlite3 always return bytestrings ...
592
- con.text_factory = bytes
593
- cur.execute("SELECT ?", (AUSTRIA,))
594
- row = cur.fetchone()
595
- assert type(row[0]) is bytes
596
- # the bytestrings will be encoded in UTF-8, unless you stored garbage in the
597
- # database ...
598
- assert row[0] == AUSTRIA.encode("utf-8")
599
-
600
- # we can also implement a custom text_factory ...
601
- # here we implement one that appends "foo" to all strings
602
- con.text_factory = lambda x: x.decode("utf-8") + "foo"
603
- cur.execute("SELECT ?", ("bar",))
604
- row = cur.fetchone()
605
- assert row[0] == "barfoo"
606
-
607
- con.close()
608
-
609
- .. attribute :: total_changes
610
-
611
- Return the total number of database rows that have been modified, inserted, or
612
- deleted since the database connection was opened.
613
-
614
-
615
518
.. method :: cursor(factory=Cursor)
616
519
617
520
Create and return a :class: `Cursor ` object.
@@ -1017,6 +920,101 @@ Connection objects
1017
920
1018
921
.. versionadded :: 3.7
1019
922
923
+ .. attribute :: in_transaction
924
+
925
+ This read-only attribute corresponds to the low-level SQLite
926
+ `autocommit mode `_.
927
+
928
+ ``True `` if a transaction is active (there are uncommitted changes),
929
+ ``False `` otherwise.
930
+
931
+ .. versionadded :: 3.2
932
+
933
+ .. attribute :: isolation_level
934
+
935
+ This attribute controls the :ref: `transaction handling
936
+ <sqlite3-controlling-transactions>` performed by :mod: `!sqlite3 `.
937
+ If set to ``None ``, transactions are never implicitly opened.
938
+ If set to one of ``"DEFERRED" ``, ``"IMMEDIATE" ``, or ``"EXCLUSIVE" ``,
939
+ corresponding to the underlying `SQLite transaction behaviour `_,
940
+ implicit :ref: `transaction management
941
+ <sqlite3-controlling-transactions>` is performed.
942
+
943
+ If not overridden by the *isolation_level * parameter of :func: `connect `,
944
+ the default is ``"" ``, which is an alias for ``"DEFERRED" ``.
945
+
946
+ .. attribute :: row_factory
947
+
948
+ A callable that accepts two arguments,
949
+ a :class: `Cursor ` object and the raw row results as a :class: `tuple `,
950
+ and returns a custom object representing an SQLite row.
951
+
952
+ Example:
953
+
954
+ .. doctest ::
955
+
956
+ >>> def dict_factory (cursor , row ):
957
+ ... col_names = [col[0 ] for col in cursor.description]
958
+ ... return {key: value for key, value in zip (col_names, row)}
959
+ >>> con = sqlite3.connect(" :memory:" )
960
+ >>> con.row_factory = dict_factory
961
+ >>> for row in con.execute(" SELECT 1 AS a, 2 AS b" ):
962
+ ... print (row)
963
+ {'a': 1, 'b': 2}
964
+
965
+ If returning a tuple doesn't suffice and you want name-based access to
966
+ columns, you should consider setting :attr: `row_factory ` to the
967
+ highly optimized :class: `sqlite3.Row ` type. :class: `Row ` provides both
968
+ index-based and case-insensitive name-based access to columns with almost no
969
+ memory overhead. It will probably be better than your own custom
970
+ dictionary-based approach or even a db_row based solution.
971
+
972
+ .. XXX what's a db_row-based solution?
973
+
974
+ .. attribute :: text_factory
975
+
976
+ A callable that accepts a :class: `bytes ` parameter and returns a text
977
+ representation of it.
978
+ The callable is invoked for SQLite values with the ``TEXT `` data type.
979
+ By default, this attribute is set to :class: `str `.
980
+ If you want to return ``bytes `` instead, set *text_factory * to ``bytes ``.
981
+
982
+ Example:
983
+
984
+ .. testcode ::
985
+
986
+ con = sqlite3.connect(":memory: ")
987
+ cur = con.cursor()
988
+
989
+ AUSTRIA = "Österreich"
990
+
991
+ # by default, rows are returned as str
992
+ cur.execute("SELECT ?", (AUSTRIA,))
993
+ row = cur.fetchone()
994
+ assert row[0] == AUSTRIA
995
+
996
+ # but we can make sqlite3 always return bytestrings ...
997
+ con.text_factory = bytes
998
+ cur.execute("SELECT ?", (AUSTRIA,))
999
+ row = cur.fetchone()
1000
+ assert type(row[0]) is bytes
1001
+ # the bytestrings will be encoded in UTF-8, unless you stored garbage in the
1002
+ # database ...
1003
+ assert row[0] == AUSTRIA.encode("utf-8")
1004
+
1005
+ # we can also implement a custom text_factory ...
1006
+ # here we implement one that appends "foo" to all strings
1007
+ con.text_factory = lambda x: x.decode("utf-8") + "foo"
1008
+ cur.execute("SELECT ?", ("bar",))
1009
+ row = cur.fetchone()
1010
+ assert row[0] == "barfoo"
1011
+
1012
+ con.close()
1013
+
1014
+ .. attribute :: total_changes
1015
+
1016
+ Return the total number of database rows that have been modified, inserted, or
1017
+ deleted since the database connection was opened.
1020
1018
1021
1019
.. _sqlite3-cursor-objects :
1022
1020
0 commit comments