【问题标题】:Convert a pyodbc.Row to a string将 pyodbc.Row 转换为字符串
【发布时间】: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 并得到无属性错误。

标签: python pyodbc


【解决方案1】:

鉴于你的 sql,我想你只是想要:

row.table_name

"table_name" 是您选择的列的名称,pyodbc 使每一列成为行对象的方便属性。

【讨论】:

  • 我不认为这回答了如何将 pyodbc.row 转换为字符串的问题。
猜你喜欢
  • 1970-01-01
  • 2019-10-10
  • 2019-01-05
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多