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

后端 未结 10 1087
闹比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:04

    python 2.7

    import pymysql
    
    conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='password', db='sakila')
    
    cur = conn.cursor()
    
    n = cur.execute('select * from actor')
    c = cur.fetchall()
    
    for i in c:
        print i[1]
    

提交回复
热议问题