我需要将 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
请您参考如下方法:
鉴于您的 sql,我认为您只想:
row.table_name
“table_name”是您选择的列的名称,pyodbc 使每一列成为行对象的方便属性。
