我有这段代码。从sql查询输出创建一个列表,使用属性名称而不是索引使用python
cursor.execute("select id, name from client")
clientids= cursor.fetchall()
clientidList = []
for clientid in clientids:
#I can do that
clientidList.append(clientid [0])
#but I can't do that.
clientidList.append(clientid ['id'])
白衣第二次尝试,我得到一个错误TypeError: 'tuple' object is not callable
任何想法,为什么这是不可能的?是否有其他方法可以实现这一点,因为当我将属性名称放在索引中时,它更加全面,完全在具有超过20列输出的查询中。 我试过this但它不适用于我
谢谢!
很酷;那个显示所有行属性:)。我刚刚找到'cursor.column_names',它实际上只是返回行名('cursor.description [n]')。很好的例子! – 2013-03-24 22:48:29
谢谢Allendar! :) – mongotop 2013-03-24 23:45:53