Convert a pyodbc.Row to a string

左心房为你撑大大i 提交于 2019-12-08 06:35:14

问题


I need to convert a pyodbc.Row to a string. The internet provides several suggestions, none of which seem to work for me.

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

The above either give an error (shown in comment) or seems to have no effect as shown in the output here:

(u'myTableName', ) # print tblName

SQL is

tablesWithId = "select table_name \
                          from INFORMATION_SCHEMA.COLUMNS \
                          where COLUMN_NAME like 'MyId' "
cursor.execute(tablesWithId)

Python 2.7


回答1:


Given your sql, I think you just want:

row.table_name

"table_name" is the name of the column you're selecting, and pyodbc makes each column a convenient attribute of the row object.



来源:https://stackoverflow.com/questions/24832954/convert-a-pyodbc-row-to-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!