Search code examples
djangocx-oracle

list Dictionary output isnt what i expected


i try to load data from py manage.py shell

with this code

def makeDictFactory(cursor):
    columnNames = [d[0] for d in cursor.description]
    def createRow(*args):
        return dict(zip(columnNames, args))
    return createRow 

import cx_Oracle
dsn_tns = cx_Oracle.makedsn('', '', sid='') 
conn = cx_Oracle.connect(user=r'', password='', dsn=dsn_tns) 
c = conn.cursor() 
c.execute("select table_name from all_tables")
c.rowfactory = makeDictFactory(c)
c.fetchall()

the output that i want is { a : b , c : d} , but when i execute the c.fetchall() it become [{a:b},{c:d}] , it cause when i put the code in backend and want to display it to front end , it will return an error like unhashable type dict , unhashable type list or context must dict or list

what should i do?


Solution

  • cursor.fetchall() is going to return a list of dictionaries using this method. This is expected. It might make more sense for you to do something like this if you want to process each row independently:

    for rowDict in c:
        do_something_with_row_dict(rowDict)