【发布时间】:2014-07-18 20:05:23
【问题描述】:
我需要将 pyodbc.Row 转换为字符串。互联网提供了一些建议,但似乎都不适合我。
row = cursor.fetchone()
#unicodedata.normalize('NFKD', row).encode('ascii','ignore') #TypeError: must be unicode, not pyodbc.Row
#row.fieldname.encode('utf8') #AttributeError: 'pyodbc.Row' object has no attribute 'fieldname'
tblName = str(row)
tblName.replace("text:u","").replace("'","")
tblName = tblName.encode('utf-8')
print tblName
以上内容要么给出错误(在注释中显示),要么似乎没有效果,如此处的输出所示:
(u'myTableName', ) # print tblName
SQL 是
tablesWithId = "select table_name \
from INFORMATION_SCHEMA.COLUMNS \
where COLUMN_NAME like 'MyId' "
cursor.execute(tablesWithId)
Python 2.7
【问题讨论】:
-
我可能弄错了,但看起来你只是想要一个字段的值。你试过
row.whatever_column_you_selected吗? -
你可能是对的。如何确定
whatever_column_you_selected是什么?代码完成没有做任何事情,我从查询中尝试了列的名称,但它说pyodbc.Row object has no attribute -
你的 sql select 语句是什么?
-
添加在上面。我试过
row.MyId并得到无属性错误。