-
Notifications
You must be signed in to change notification settings - Fork 361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fetch cursor rows as list of dict #410
Comments
This seems a common request. In the meantime, using a rowfactory would allow all the current fetch functions to return dicts. Using the one-liner from here, you could do: import cx_Oracle
conn = cx_Oracle.connect("cj", "cj", "localhost/orclpdb1", encoding="UTF-8")
with conn.cursor() as cursor:
cursor.execute("select * from locations")
cursor.rowfactory = lambda *args: dict(zip([d[0] for d in cursor.description], args))
data = cursor.fetchall()
print(data) will print:
Update: Oracle's JSON functions such as JSON_OBJECT may be useful in some cases, for example:
will return:
|
This is a very common scenario.Take a look on this question, a guy just asked today. He just did:
|
Is this functionality planned for 8.1? |
We're considering other enhancements so I don't know if this will make 8.1 or not. Stay tuned! |
Be advised that there is an issue that was filed that claims rowfactory has a memory leak #623 |
@troyswanson Thanks for the heads up |
Convert a cursor result set into a list of dictionary is a very common pattern, mainly when you are coding APIs that returns data as json. A list of dict is a versatile data structure to deal with other things like csv files or pandas dataframes.
The following implementation show how we can do it using cx_Oracle 7.3
I think if cx_Oracle bring this by default, it can be a more powerful and versatile tool.
The text was updated successfully, but these errors were encountered: