[Python-checkins] gh-96168: Improve sqlite3 dict_factory example (GH-96457)

miss-islington webhook-mailer at python.org
Thu Sep 1 17:55:43 EDT 2022


https://github.com/python/cpython/commit/fca8e94dbfc4a9164007063ddf7f2d72e75a4dc2
commit: fca8e94dbfc4a9164007063ddf7f2d72e75a4dc2
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-09-01T14:55:37-07:00
summary:

gh-96168: Improve sqlite3 dict_factory example (GH-96457)


Co-authored-by: C.A.M. Gerlach <CAM.Gerlach at Gerlach.CAM>
Co-authored-by: Ezio Melotti <ezio.melotti at gmail.com>
(cherry picked from commit 91f40f3f78d6016a283989e32ec3d1fb61bcebca)

Co-authored-by: Erlend E. Aasland <erlend.aasland at innova.no>

files:
M Doc/library/sqlite3.rst

diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst
index fc288f6f7399..aace339e3a4d 100644
--- a/Doc/library/sqlite3.rst
+++ b/Doc/library/sqlite3.rst
@@ -588,25 +588,16 @@ Connection objects
 
       Example:
 
-      .. testcode::
-
-         def dict_factory(cursor, row):
-             d = {}
-             for idx, col in enumerate(cursor.description):
-                 d[col[0]] = row[idx]
-             return d
-
-         con = sqlite3.connect(":memory:")
-         con.row_factory = dict_factory
-         cur = con.execute("SELECT 1 AS a")
-         print(cur.fetchone()["a"])
-
-         con.close()
-
-      .. testoutput::
-         :hide:
+      .. doctest::
 
-         1
+         >>> def dict_factory(cursor, row):
+         ...     col_names = [col[0] for col in cursor.description]
+         ...     return {key: value for key, value in zip(col_names, row)}
+         >>> con = sqlite3.connect(":memory:")
+         >>> con.row_factory = dict_factory
+         >>> for row in con.execute("SELECT 1 AS a, 2 AS b"):
+         ...     print(row)
+         {'a': 1, 'b': 2}
 
       If returning a tuple doesn't suffice and you want name-based access to
       columns, you should consider setting :attr:`row_factory` to the



More information about the Python-checkins mailing list