How can I make cx-oracle bind the results of a query to a dictionary rather than a tuple?

后端 未结 2 1488
日久生厌
日久生厌 2021-02-02 16:16

Here is my code. I would like to find a way to have results from a query returned as a list of dictionaries rather than list of tuples. It seems like cx_oracle supports this wit

2条回答
  •  青春惊慌失措
    2021-02-02 16:46

    Here is a quick and dirty. Feel post a better way.

    def connect():  
        dsn = cx_Oracle.makedsn("host", 1521, "sid")
        orcl = cx_Oracle.connect('scott/tiger@' + dsn)
        curs = orcl.cursor()
        sql = "select * from sometable"
        curs.execute(sql)
        fieldNumber = 0
        fieldNames={}
        for desc in curs.description:
            fieldNames[desc[0]]=fieldNumber
            fieldNumber+=1
        result = curs.fetchall()
        for row in result:
            print str(row[fieldNames['CATEGORY']])
        curs.close()
    

提交回复
热议问题