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

后端 未结 10 1104
闹比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 15:00

    you must look for something called " dictionary in cursor "

    i'm using mysql connector and i have to add this parameter to my cursor , so i can use my columns names instead of index's

    db = mysql.connector.connect(
        host=db_info['mysql_host'],
        user=db_info['mysql_user'],
        passwd=db_info['mysql_password'],
        database=db_info['mysql_db'])
    
    cur = db.cursor()
    
    cur = db.cursor( buffered=True , dictionary=True)
    

提交回复
热议问题