File tree 1 file changed +9
-18
lines changed
1 file changed +9
-18
lines changed Original file line number Diff line number Diff line change @@ -588,25 +588,16 @@ Connection objects
588
588
589
589
Example:
590
590
591
- .. testcode ::
592
-
593
- def dict_factory(cursor, row):
594
- d = {}
595
- for idx, col in enumerate(cursor.description):
596
- d[col[0]] = row[idx]
597
- return d
598
-
599
- con = sqlite3.connect(":memory: ")
600
- con.row_factory = dict_factory
601
- cur = con.execute("SELECT 1 AS a")
602
- print(cur.fetchone()["a"])
603
-
604
- con.close()
605
-
606
- .. testoutput ::
607
- :hide:
591
+ .. doctest ::
608
592
609
- 1
593
+ >>> def dict_factory (cursor , row ):
594
+ ... col_names = [col[0 ] for col in cursor.description]
595
+ ... return {key: value for key, value in zip (col_names, row)}
596
+ >>> con = sqlite3.connect(" :memory:" )
597
+ >>> con.row_factory = dict_factory
598
+ >>> for row in con.execute(" SELECT 1 AS a, 2 AS b" ):
599
+ ... print (row)
600
+ {'a': 1, 'b': 2}
610
601
611
602
If returning a tuple doesn't suffice and you want name-based access to
612
603
columns, you should consider setting :attr: `row_factory ` to the
You can’t perform that action at this time.
0 commit comments