如何在查询Python中按名称获取字段? [英] How to get fields by name in query Python?
本文介绍了如何在查询Python中按名称获取字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Python脚本中使用Mysql Connection.
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] //
相反,我想按如下名称获取字段:print row["name"]
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'])
我希望这对您有帮助
这篇关于如何在查询Python中按名称获取字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文