@@ -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.
@@ -1242,17 +1241,21 @@ Cursor objects
1242
1241
>>> cur.connection == con
1243
1242
True
1244
1243
1244
+ .. The sqlite3.Row example used to be a how-to. It has now been incorporated
1245
+ into the Row reference. We keep the anchor here in order not to break
1246
+ existing links.
1247
+
1248
+ .. _sqlite3-columns-by-name :
1245
1249
.. _sqlite3-row-objects :
1246
1250
1247
1251
Row objects
1248
1252
^^^^^^^^^^^
1249
1253
1250
1254
.. class :: Row
1251
1255
1252
- A :class: `Row ` instance serves as a highly optimized
1256
+ A :class: `! Row ` instance serves as a highly optimized
1253
1257
:attr: `~Connection.row_factory ` for :class: `Connection ` objects.
1254
- It tries to mimic a :class: `tuple ` in most of its features,
1255
- and supports iteration, :func: `repr `, equality testing, :func: `len `,
1258
+ It supports iteration, equality testing, :func: `len `,
1256
1259
and :term: `mapping ` access by column name and index.
1257
1260
1258
1261
Two row objects compare equal if have equal columns and equal members.
@@ -1266,45 +1269,18 @@ Row objects
1266
1269
.. versionchanged :: 3.5
1267
1270
Added support of slicing.
1268
1271
1269
- Let's assume we initialize a table as in the example given above ::
1272
+ Example ::
1270
1273
1271
- con = sqlite3.connect(":memory:")
1272
- cur = con.cursor()
1273
- cur.execute('''create table stocks
1274
- (date text, trans text, symbol text,
1275
- qty real, price real)''')
1276
- cur.execute("""insert into stocks
1277
- values ('2006-01-05','BUY','RHAT',100,35.14)""")
1278
- con.commit()
1279
- cur.close()
1280
-
1281
- Now we plug :class: `Row ` in::
1282
-
1283
- >>> con.row_factory = sqlite3.Row
1284
- >>> cur = con.cursor()
1285
- >>> cur.execute('select * from stocks')
1286
- <sqlite3.Cursor object at 0x7f4e7dd8fa80>
1287
- >>> r = cur.fetchone()
1288
- >>> type(r)
1289
- <class 'sqlite3.Row'>
1290
- >>> tuple(r)
1291
- ('2006-01-05', 'BUY', 'RHAT', 100.0, 35.14)
1292
- >>> len(r)
1293
- 5
1294
- >>> r[2]
1295
- 'RHAT'
1296
- >>> r.keys()
1297
- ['date', 'trans', 'symbol', 'qty', 'price']
1298
- >>> r['qty']
1299
- 100.0
1300
- >>> for member in r:
1301
- ... print(member)
1302
- ...
1303
- 2006-01-05
1304
- BUY
1305
- RHAT
1306
- 100.0
1307
- 35.14
1274
+ >>> con = sqlite3.connect(":memory:")
1275
+ >>> con.row_factory = sqlite3.Row
1276
+ >>> res = con.execute("SELECT 'Earth' AS name, 6378 AS radius")
1277
+ >>> row = res.fetchone()
1278
+ >>> row.keys()
1279
+ ['name', 'radius']
1280
+ >>> row[0], row["name"] # Access by index and name.
1281
+ ('Earth', 'Earth')
1282
+ >>> row["RADIUS"] # Column names are case-insensitive.
1283
+ 6378
1308
1284
1309
1285
1310
1286
.. _sqlite3-blob-objects :
@@ -1726,20 +1702,6 @@ directly using only a single call on the :class:`Connection` object.
1726
1702
.. literalinclude :: ../includes/sqlite3/shortcut_methods.py
1727
1703
1728
1704
1729
- .. _sqlite3-columns-by-name :
1730
-
1731
- Accessing columns by name instead of by index
1732
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1733
-
1734
- One useful feature of the :mod: `!sqlite3 ` module is the built-in
1735
- :class: `sqlite3.Row ` class designed to be used as a row factory.
1736
-
1737
- Rows wrapped with this class can be accessed both by index (like tuples) and
1738
- case-insensitively by name:
1739
-
1740
- .. literalinclude :: ../includes/sqlite3/rowclass.py
1741
-
1742
-
1743
1705
.. _sqlite3-connection-context-manager :
1744
1706
1745
1707
Using the connection as a context manager
0 commit comments