[CONPY-56] Support dictionary option in cursor class Created: 2020-04-12  Updated: 2020-04-14  Resolved: 2020-04-14

Status: Closed
Project: MariaDB Connector/Python
Component/s: DBAPI 2.0
Affects Version/s: 0.9.56
Fix Version/s: 0.9.57

Type: New Feature Priority: Major
Reporter: Georg Richter Assignee: Georg Richter
Resolution: Fixed Votes: 0
Labels: Compatibility


 Description   

Support returning Dict class instead of default list when fetching rows:

import mariadb
 
connection=mariadb.connect(user="user")
cursor=connection.cursor(dictionary=True)
cursor.execute("SELECT 1 as foo")
row= cursor.fetchone
print(row)

should return

{'foo': 1}



 Comments   
Comment by Georg Richter [ 2020-04-12 ]

It should be documented, that this option only exists for compatibility reasons. If possible dictionary=True should be avoided, since it may lead to inconsistency: While in a dictionary keys are unique, they are not unique in a result set:

>>> cursor=conn.cursor(dictionary=True)
>>> cursor.execute("select 1 as foo, 'bar' as foo")
>>> cursor.fetchone()
{'foo': 'bar'}
>>> cursor.execute("select 1,2,1")
>>> cursor.fetchone()
{'1': 1, '2': 2}

Comment by Georg Richter [ 2020-04-14 ]

rev. e873f87fabe99410dab5e736eb6f50104fbd1999

Generated at Thu Feb 08 03:29:55 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.