How to retrieve SQL result column value using column name in Python?

后端 未结 10 1090
闹比i
闹比i 2020-11-27 14:05

Is there a way to retrieve SQL result column value using column name instead of column index in Python? I\'m using Python 3 with mySQL. The syntax I\'m looking for is pretty

10条回答
  •  抹茶落季
    2020-11-27 14:52

    You didn't provide many details, but you could try something like this:

    # conn is an ODBC connection to the DB
    dbCursor = conn.cursor()
    sql = ('select field1, field2 from table') 
    dbCursor = conn.cursor()
    dbCursor.execute(sql)
    for row in dbCursor:
        # Now you should be able to access the fields as properties of "row"
        myVar1 = row.field1
        myVar2 = row.field2
    conn.close()
    

提交回复
热议问题