Skip to content
Snippets Groups Projects
Unverified Commit fca8e94d authored by Miss Islington (bot)'s avatar Miss Islington (bot) Committed by GitHub
Browse files

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



Co-authored-by: default avatarC.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Co-authored-by: default avatarEzio Melotti <ezio.melotti@gmail.com>
(cherry picked from commit 91f40f3f)

Co-authored-by: default avatarErlend E. Aasland <erlend.aasland@innova.no>
parent 58359113
Branches
Tags
No related merge requests found
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment