How do I get fields by name in the Python query?

advertisements

I use Mysql Connection in Python script.

How can I get results from table by names?

cursor = conn.cursor()
cursor.execute("SELECT * FROM local")

Now I do this by index:

results = cursor.fetchall()
for row in results:
   print row[0] //

Instead that I wanna get fields by names like as: print row["name"]


Instead you can use pandas library

import pandas as pd
sql = "SELECT * FROM local"
df = pd.read_sql(sql,conn)

for index,row in df.iterrows():
    print(row['name'])

I hope this helps