4

I'm using pyodbc to connect sqlserver and below is my connection string..Everything is proper but the results are returned as a unicode string..I have the CHARSET=UTF8 in the connection string but still its returning as unicode string?

Is there any way that I can limit it using the connection paramter itself?

I don't want to call a extra function to convert my unicode to normal strings.

import pyodbc as p

connstr= 'DRIVER={SQL Server};SERVER=USERNAME\SQLEXPRESS;DATABASE=TEST;Trusted_Connection=yes;unicode_results=True;CHARSET=UTF8'
conn = p.connect(connstr)
print conn
cursor = conn.cursor()
result = cursor.execute("select * from employee1")
for each in result:
    print each
user1050619
  • 19,822
  • 85
  • 237
  • 413

1 Answers1

6

You can not handle this issue in the connection string. SQL Server doesn't have a CHARSET property in it's odbc connection settings, so that won't do you any good.

The overall issue you are having is that the data IS unicode in the database. The data type for that column is nvarchar, it is an extended (UTF-16... could be UC-2 in windows, can't remember) data type to include international data characters.

Your options are to convert the data via cast in the select query, e.g.:

SELECT CAST(fieldname AS VARCHAR) AS fieldname

or convert it in python, e.g.:

# to utf-8
row.fieldname.encode('utf8')

# to ascii, ignore non-utf-8 characters
row.fieldname.encode('ascii', 'ignore')

# to ascii, replace non-utf-8 characters with ?
row.fieldname.encode('ascii', 'replace')

If you don't need international characters, then you could store the data in varchar instead of nvarchar.

dkleehammer
  • 431
  • 5
  • 17
  • 3
    Also, for anyone else needing this. If you have international characters and you use the cast in the database, you will get strange characters and the actual utf-16/uc-2 character will be lost. – dkleehammer Jun 20 '13 at 15:23
  • 2
    Any idea why I can select an nvarchar(255) field but not a nvarchar with no size specified? I get `Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier` – Matt Apr 16 '14 at 18:45