@@ -207,7 +207,6 @@ inserted data and retrieved values from it in multiple ways.
207
207
* :ref: `sqlite3-placeholders `
208
208
* :ref: `sqlite3-adapters `
209
209
* :ref: `sqlite3-converters `
210
- * :ref: `sqlite3-columns-by-name `
211
210
* :ref: `sqlite3-connection-context-manager `
212
211
213
212
* :ref: `sqlite3-explanation ` for in-depth background on transaction control.
@@ -1017,17 +1016,21 @@ Cursor objects
1017
1016
>>> cur.connection == con
1018
1017
True
1019
1018
1019
+ .. The sqlite3.Row example used to be a how-to. It has now been incorporated
1020
+ into the Row reference. We keep the anchor here in order not to break
1021
+ existing links.
1022
+
1023
+ .. _sqlite3-columns-by-name :
1020
1024
.. _sqlite3-row-objects :
1021
1025
1022
1026
Row objects
1023
1027
^^^^^^^^^^^
1024
1028
1025
1029
.. class :: Row
1026
1030
1027
- A :class: `Row ` instance serves as a highly optimized
1031
+ A :class: `! Row ` instance serves as a highly optimized
1028
1032
:attr: `~Connection.row_factory ` for :class: `Connection ` objects.
1029
- It tries to mimic a :class: `tuple ` in most of its features,
1030
- and supports iteration, :func: `repr `, equality testing, :func: `len `,
1033
+ It supports iteration, equality testing, :func: `len `,
1031
1034
and :term: `mapping ` access by column name and index.
1032
1035
1033
1036
Two row objects compare equal if have equal columns and equal members.
@@ -1041,45 +1044,18 @@ Row objects
1041
1044
.. versionchanged :: 3.5
1042
1045
Added support of slicing.
1043
1046
1044
- Let's assume we initialize a table as in the example given above ::
1047
+ Example ::
1045
1048
1046
- con = sqlite3.connect(":memory:")
1047
- cur = con.cursor()
1048
- cur.execute('''create table stocks
1049
- (date text, trans text, symbol text,
1050
- qty real, price real)''')
1051
- cur.execute("""insert into stocks
1052
- values ('2006-01-05','BUY','RHAT',100,35.14)""")
1053
- con.commit()
1054
- cur.close()
1055
-
1056
- Now we plug :class: `Row ` in::
1057
-
1058
- >>> con.row_factory = sqlite3.Row
1059
- >>> cur = con.cursor()
1060
- >>> cur.execute('select * from stocks')
1061
- <sqlite3.Cursor object at 0x7f4e7dd8fa80>
1062
- >>> r = cur.fetchone()
1063
- >>> type(r)
1064
- <class 'sqlite3.Row'>
1065
- >>> tuple(r)
1066
- ('2006-01-05', 'BUY', 'RHAT', 100.0, 35.14)
1067
- >>> len(r)
1068
- 5
1069
- >>> r[2]
1070
- 'RHAT'
1071
- >>> r.keys()
1072
- ['date', 'trans', 'symbol', 'qty', 'price']
1073
- >>> r['qty']
1074
- 100.0
1075
- >>> for member in r:
1076
- ... print(member)
1077
- ...
1078
- 2006-01-05
1079
- BUY
1080
- RHAT
1081
- 100.0
1082
- 35.14
1049
+ >>> con = sqlite3.connect(":memory:")
1050
+ >>> con.row_factory = sqlite3.Row
1051
+ >>> res = con.execute("SELECT 'Earth' AS name, 6378 AS radius")
1052
+ >>> row = res.fetchone()
1053
+ >>> row.keys()
1054
+ ['name', 'radius']
1055
+ >>> row[0], row["name"] # Access by index and name.
1056
+ ('Earth', 'Earth')
1057
+ >>> row["RADIUS"] # Column names are case-insensitive.
1058
+ 6378
1083
1059
1084
1060
1085
1061
PrepareProtocol objects
@@ -1429,20 +1405,6 @@ directly using only a single call on the :class:`Connection` object.
1429
1405
.. literalinclude :: ../includes/sqlite3/shortcut_methods.py
1430
1406
1431
1407
1432
- .. _sqlite3-columns-by-name :
1433
-
1434
- Accessing columns by name instead of by index
1435
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1436
-
1437
- One useful feature of the :mod: `!sqlite3 ` module is the built-in
1438
- :class: `sqlite3.Row ` class designed to be used as a row factory.
1439
-
1440
- Rows wrapped with this class can be accessed both by index (like tuples) and
1441
- case-insensitively by name:
1442
-
1443
- .. literalinclude :: ../includes/sqlite3/rowclass.py
1444
-
1445
-
1446
1408
.. _sqlite3-connection-context-manager :
1447
1409
1448
1410
Using the connection as a context manager
0 commit comments