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

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

    This post is old but may come up via searching.

    Now you can use mysql.connector to retrive a dictionary as shown here: https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursordict.html

    Here is the example on the mysql site:

    cnx = mysql.connector.connect(database='world')
    cursor = cnx.cursor(dictionary=True)
    cursor.execute("SELECT * FROM country WHERE Continent = 'Europe'")
    
    print("Countries in Europe:")
    for row in cursor:
        print("* {Name}".format(Name=row['Name']))
    

提交回复
热议问题