Al Lelopath
Al Lelopath

Reputation: 6778

Convert a pyodbc.Row to a string

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

Upvotes: 4

Views: 17147

Answers (1)

Gerrat
Gerrat

Reputation: 29690

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.

Upvotes: 4

Related Questions