Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a pyodbc.Row to a string

Tags:

python

pyodbc

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

like image 833
Al Lelopath Avatar asked Sep 03 '25 17:09

Al Lelopath


1 Answers

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.

like image 123
Gerrat Avatar answered Sep 07 '25 06:09

Gerrat