@@ -180,7 +180,7 @@ To extract the column names from a query you can use code like:
180180
181181 with connection.cursor() as cursor:
182182 cursor.execute(" select * from locations" )
183- columns = [col[ 0 ] for col in cursor.description]
183+ columns = [col.name for col in cursor.description]
184184 print (columns)
185185 for r in cursor:
186186 print (r)
@@ -192,6 +192,22 @@ This will print::
192192 (1100, '93091 Calle della Testa', '10934', 'Venice', None, 'IT')
193193 . . .
194194
195+ **Changing Column Names to Lowercase **
196+
197+ To change all column names to lowercase you could do:
198+
199+ .. code-block :: python
200+
201+ cursor.execute(" select * from locations where location_id = 1000" )
202+
203+ columns = [col.name.lower() for col in cursor.description]
204+ print (columns)
205+
206+ The output is::
207+
208+ ['location_id', 'street_address', 'postal_code', 'city', 'state_province',
209+ 'country_id']
210+
195211.. _defaultfetchtypes :
196212
197213Fetch Data Types
@@ -452,13 +468,15 @@ the database. The :meth:`Cursor.rowfactory` method is called with the tuple
452468fetched from the database before it is returned to the application. The method
453469can convert the tuple to a different value.
454470
471+ **Fetching Rows as Dictionaries **
472+
455473For example, to fetch each row of a query as a dictionary:
456474
457475.. code-block :: python
458476
459477 cursor.execute(" select * from locations where location_id = 1000" )
460478
461- columns = [col[ 0 ] for col in cursor.description]
479+ columns = [col.name for col in cursor.description]
462480 cursor.rowfactory = lambda * args : dict (zip (columns, args))
463481 data = cursor.fetchone()
464482 print (data)
@@ -484,8 +502,11 @@ only one of the similarly named columns will be included in the dictionary:
484502 dogs.color
485503 from cats, dogs
486504
505+ **Example with an Output Type Handler, Outconverter, and Row Factory **
506+
487507An example showing an :ref: `output type handler <outputtypehandlers >`, an
488- :ref: `outconverter <outconverters >`, and a row factory is:
508+ :ref: `outconverter <outconverters >`, and a :ref: `row factory <rowfactories >`
509+ is:
489510
490511.. code-block :: python
491512
@@ -505,17 +526,17 @@ An example showing an :ref:`output type handler <outputtypehandlers>`, an
505526
506527 cursor.execute(" select 123 as col1, 'abc' as col2 from dual" )
507528
508- columns = [col[ 0 ] for col in cursor.description]
529+ columns = [col.name.lower() for col in cursor.description]
509530 cursor.rowfactory = lambda * args : dict (zip (columns, args))
510531 for r in cursor.fetchall():
511532 print (r)
512533
513534 The database converts the number to a string before it is returned to
514- python-oracledb. The outconverter appends "was a string" to this value.
515- Finally the row factory changes the complete row to a dictionary. The output
516- is::
535+ python-oracledb. The outconverter appends "was a string" to this value. The
536+ column names are converted to lowercase. Finally, the row factory changes the
537+ complete row to a dictionary. The output is::
517538
518- {'COL1 ': '123 was a string', 'COL2 ': 'abc'}
539+ {'col1 ': '123 was a string', 'col2 ': 'abc'}
519540
520541.. _numberprecision :
521542
0 commit comments