将 pyodbc.Row 转换为字符串 [英] Convert a pyodbc.Row to a string
本文介绍了将 pyodbc.Row 转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将 pyodbc.Row 转换为字符串.互联网提供了一些建议,但似乎没有一个适合我.
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 是
tablesWithId = "select table_name \
from INFORMATION_SCHEMA.COLUMNS \
where COLUMN_NAME like 'MyId' "
cursor.execute(tablesWithId)
Python 2.7
推荐答案
鉴于你的 sql,我想你只是想要:
Given your sql, I think you just want:
row.table_name
"table_name
" 是您选择的列的名称,pyodbc 使每一列成为行对象的方便属性.
"table_name
" is the name of the column you're selecting, and pyodbc makes each column a convenient attribute of the row object.
这篇关于将 pyodbc.Row 转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文