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 @@ -546,25 +546,16 @@ Connection objects
546
546
547
547
Example:
548
548
549
- .. testcode ::
550
-
551
- def dict_factory(cursor, row):
552
- d = {}
553
- for idx, col in enumerate(cursor.description):
554
- d[col[0]] = row[idx]
555
- return d
556
-
557
- con = sqlite3.connect(":memory: ")
558
- con.row_factory = dict_factory
559
- cur = con.execute("SELECT 1 AS a")
560
- print(cur.fetchone()["a"])
561
-
562
- con.close()
563
-
564
- .. testoutput ::
565
- :hide:
549
+ .. doctest ::
566
550
567
- 1
551
+ >>> def dict_factory (cursor , row ):
552
+ ... col_names = [col[0 ] for col in cursor.description]
553
+ ... return {key: value for key, value in zip (col_names, row)}
554
+ >>> con = sqlite3.connect(" :memory:" )
555
+ >>> con.row_factory = dict_factory
556
+ >>> for row in con.execute(" SELECT 1 AS a, 2 AS b" ):
557
+ ... print (row)
558
+ {'a': 1, 'b': 2}
568
559
569
560
If returning a tuple doesn't suffice and you want name-based access to
570
561
columns, you should consider setting :attr: `row_factory ` to the
You can’t perform that action at this time.
0 commit comments