@@ -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.
@@ -1255,17 +1254,21 @@ Cursor objects
1255
1254
>>> cur.connection == con
1256
1255
True
1257
1256
1257
+ .. The sqlite3.Row example used to be a how-to. It has now been incorporated
1258
+ into the Row reference. We keep the anchor here in order not to break
1259
+ existing links.
1260
+
1261
+ .. _sqlite3-columns-by-name :
1258
1262
.. _sqlite3-row-objects :
1259
1263
1260
1264
Row objects
1261
1265
^^^^^^^^^^^
1262
1266
1263
1267
.. class :: Row
1264
1268
1265
- A :class: `Row ` instance serves as a highly optimized
1269
+ A :class: `! Row ` instance serves as a highly optimized
1266
1270
:attr: `~Connection.row_factory ` for :class: `Connection ` objects.
1267
- It tries to mimic a :class: `tuple ` in most of its features,
1268
- and supports iteration, :func: `repr `, equality testing, :func: `len `,
1271
+ It supports iteration, equality testing, :func: `len `,
1269
1272
and :term: `mapping ` access by column name and index.
1270
1273
1271
1274
Two row objects compare equal if have equal columns and equal members.
@@ -1279,45 +1282,18 @@ Row objects
1279
1282
.. versionchanged :: 3.5
1280
1283
Added support of slicing.
1281
1284
1282
- Let's assume we initialize a table as in the example given above ::
1285
+ Example ::
1283
1286
1284
- con = sqlite3.connect(":memory:")
1285
- cur = con.cursor()
1286
- cur.execute('''create table stocks
1287
- (date text, trans text, symbol text,
1288
- qty real, price real)''')
1289
- cur.execute("""insert into stocks
1290
- values ('2006-01-05','BUY','RHAT',100,35.14)""")
1291
- con.commit()
1292
- cur.close()
1293
-
1294
- Now we plug :class: `Row ` in::
1295
-
1296
- >>> con.row_factory = sqlite3.Row
1297
- >>> cur = con.cursor()
1298
- >>> cur.execute('select * from stocks')
1299
- <sqlite3.Cursor object at 0x7f4e7dd8fa80>
1300
- >>> r = cur.fetchone()
1301
- >>> type(r)
1302
- <class 'sqlite3.Row'>
1303
- >>> tuple(r)
1304
- ('2006-01-05', 'BUY', 'RHAT', 100.0, 35.14)
1305
- >>> len(r)
1306
- 5
1307
- >>> r[2]
1308
- 'RHAT'
1309
- >>> r.keys()
1310
- ['date', 'trans', 'symbol', 'qty', 'price']
1311
- >>> r['qty']
1312
- 100.0
1313
- >>> for member in r:
1314
- ... print(member)
1315
- ...
1316
- 2006-01-05
1317
- BUY
1318
- RHAT
1319
- 100.0
1320
- 35.14
1287
+ >>> con = sqlite3.connect(":memory:")
1288
+ >>> con.row_factory = sqlite3.Row
1289
+ >>> res = con.execute("SELECT 'Earth' AS name, 6378 AS radius")
1290
+ >>> row = res.fetchone()
1291
+ >>> row.keys()
1292
+ ['name', 'radius']
1293
+ >>> row[0], row["name"] # Access by index and name.
1294
+ ('Earth', 'Earth')
1295
+ >>> row["RADIUS"] # Column names are case-insensitive.
1296
+ 6378
1321
1297
1322
1298
1323
1299
.. _sqlite3-blob-objects :
@@ -1766,20 +1742,6 @@ directly using only a single call on the :class:`Connection` object.
1766
1742
.. literalinclude :: ../includes/sqlite3/shortcut_methods.py
1767
1743
1768
1744
1769
- .. _sqlite3-columns-by-name :
1770
-
1771
- Accessing columns by name instead of by index
1772
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1773
-
1774
- One useful feature of the :mod: `!sqlite3 ` module is the built-in
1775
- :class: `sqlite3.Row ` class designed to be used as a row factory.
1776
-
1777
- Rows wrapped with this class can be accessed both by index (like tuples) and
1778
- case-insensitively by name:
1779
-
1780
- .. literalinclude :: ../includes/sqlite3/rowclass.py
1781
-
1782
-
1783
1745
.. _sqlite3-connection-context-manager :
1784
1746
1785
1747
Using the connection as a context manager
0 commit comments