【问题标题】:list Dictionary output isnt what i expected列表字典输出不是我所期望的
【发布时间】:2020-03-04 22:07:20
【问题描述】:

我尝试从 py manage.py shell 加载数据

使用此代码

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()

我想要的输出是 { a : b , c : d} ,但是当我执行 c.fetchall() 时它变成 [{a:b},{c:d}] ,当我放后端的代码想要显示到前端,它会返回一个错误,比如 unhashable type dict , unhashable type list or context must dict or list

我该怎么办?

【问题讨论】:

    标签: django cx-oracle


    【解决方案1】:

    cursor.fetchall() 将使用此方法返回一个列表 字典。这是意料之中的。如果您想独立处理每一行,这样做可能更有意义:

    for rowDict in c:
        do_something_with_row_dict(rowDict)
    

    【讨论】:

    • sry 回到办公室,这就是我需要的答案,谢谢..
    猜你喜欢
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 2014-08-25
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多