有没有办法在Python中使用列名而不是列索引检索SQL结果列值?我用mySQL使用Python 3。我在寻找的语法是非常像Java结构:如何在Python中使用列名检索SQL结果列值?
Object id = rs.get("CUSTOMER_ID");
我有一个表,相当数量的列,它是一个真正的痛苦不断制定出每列,我需要索引访问。此外,索引使我的代码难以阅读。
谢谢!
有没有办法在Python中使用列名而不是列索引检索SQL结果列值?我用mySQL使用Python 3。我在寻找的语法是非常像Java结构:如何在Python中使用列名检索SQL结果列值?
Object id = rs.get("CUSTOMER_ID");
我有一个表,相当数量的列,它是一个真正的痛苦不断制定出每列,我需要索引访问。此外,索引使我的代码难以阅读。
谢谢!
的MySQLdb模块具有DictCursor:
使用这样的(从Writing MySQL Scripts with Python DB-API拍摄):
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("SELECT name, category FROM animal")
result_set = cursor.fetchall()
for row in result_set:
print "%s, %s" % (row["name"], row["category"])
编辑:根据user1305650这也适用于pymysql
。
尝试了这一点,它不工作 - 得到一个错误“TypeError:元组索引必须是整数,而不是str” – Thomas 2012-04-17 16:54:00
您是否正在使用'MySQLdb'模块?你是否像这样创建了游标:'cursor = conn.cursor(MySQLdb.cursors.DictCursor)'? – 2012-04-17 16:57:21
我的不好!我没有按照你的帖子创建光标。它现在可以工作 - 我只需要将MySQLdb.cursors.DictCursor更改为pymysql.cursors.DictCursor,因为我使用了pymysql。谢谢! – Thomas 2012-04-17 17:07:21
当然有。在Python 2.7.2 + ...
import MySQLdb as mdb
con = mdb.connect('localhost', 'user', 'password', 'db');
cur = con.cursor()
cur.execute('SELECT Foo, Bar FROM Table')
for i in range(int(cur.numrows)):
foo, bar = cur.fetchone()
print 'foo = %s' % foo
print 'bar = %s' % bar
-1。对不起,严重怀疑这是OP的意思。 – 2012-04-17 16:30:59
你认为他想要做什么?按列名选择值并将它们分配给变量看起来非常简单。 – TaoJoannes 2012-04-17 16:39:07
看起来他想要一个独立于列顺序的解决方案。 – 2012-04-17 16:53:28
您没有提供很多细节,但你可以尝试这样的事:
# conn is an ODBC connection to the DB
dbCursor = conn.cursor()
sql = ('select field1, field2 from table')
dbCursor = conn.cursor()
dbCursor.execute(sql)
for row in dbCursor:
# Now you should be able to access the fields as properties of "row"
myVar1 = row.field1
myVar2 = row.field2
conn.close()
蟒蛇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]
OP要按属性不索引 – levi 2017-05-15 17:20:15
这个职位是旧的,但可能出现通过搜索。
现在你可以使用mysql.connector以检索字典如下所示: https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursordict.html
这里是在MySQL网站的例子:
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']
进口pymysql
# Open database connection
db = pymysql.connect("localhost","root","","gkdemo1")
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT * from user")
# Get the fields name (only once!)
field_name = [field[0] for field in cursor.description]
# Fetch a single row using fetchone() method.
values = cursor.fetchone()
# create the row dictionary to be able to call row['login']
**row = dict(zip(field_name, values))**
# print the dictionary
print(row)
# print specific field
print(**row['login']**)
# print all field
for key in row:
print(**key," = ",row[key]**)
# close database connection
db.close()
你可能希望提供更多的细节,比如你正在尝试的一些代码,你正在使用的python包。 – Shekhar 2012-04-17 16:27:53