From 430bb2a850eb7f9674fbd12bf59abbb3c6163a36 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 31 Aug 2022 10:45:02 +0200 Subject: [PATCH 1/3] gh-96168: Improve sqlite3 dict_factory example - use dict comprehension - use doctest iso. testcode, for visualising the effect of the row factory - SELECT with multiple columns, so the effect of the row factory becomes clearer --- Doc/library/sqlite3.rst | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index 7ac7162c252716..42f7b99d6dc0de 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -601,25 +601,15 @@ 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): + ... return {col[0]: row[i] for i, col in enumerate(cursor.description)} + >>> 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 From 4fc8b36e3f3fc3a42688d6f1725b044e6b6b44a3 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 31 Aug 2022 13:00:53 +0200 Subject: [PATCH 2/3] Address review: improve row factory --- Doc/library/sqlite3.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index 42f7b99d6dc0de..4de0ff92bb8fae 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -604,7 +604,8 @@ Connection objects .. doctest:: >>> def dict_factory(cursor, row): - ... return {col[0]: row[i] for i, col in enumerate(cursor.description)} + ... 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"): From c4c96562271e827383863bc3d14b70236609d8e7 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 1 Sep 2022 22:30:55 +0200 Subject: [PATCH 3/3] Address review: fix SQL style Co-authored-by: C.A.M. Gerlach --- Doc/library/sqlite3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index 4de0ff92bb8fae..b188ca4127f5b5 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -608,7 +608,7 @@ Connection objects ... 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"): + >>> for row in con.execute("SELECT 1 AS a, 2 AS b"): ... print(row) {'a': 1, 'b': 2}