0

I've successfully setup a connection from a stock raspberry pi 4 to our local database running on windows 10.

In the terminal if I type:

tsql -S *servername* -U *username* -P *password*
select * from testlist_table
go

I get the result for the query and all is good for that test. It's probably worth mentioning I recieve back after the first tsql line:

default encoding is en_us.utf-8 or something odd like this and then: using default encoding utf-8,

When using python code if I type

import pyodbc
cnxn = pyodbc.connect("Driver={freeTDS};Server=*servername*;DATABASE=*databasename*;UID=*userid*;pwd=*pwd*")

cursor = cnxn.cursor()
cursor.execute("Select * from TestList_Table")

I get the following error:

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.

Any ideas? I've tried setting encoding / decoding but that hasn't help. If I select a particular field so far that's worked. Not excited to try and change all the code as I have a lot of columns and am referencing by column number sometimes in a big mess of code.

1

1 Answer 1

1

I'm going to assume you're running a relatively recent version of FreeTDS (version 0.95 or above; you can find your version with tsql -C). If you're using an earlier version and this doesn't work, trying changing the TDS_Version to 7.2 or 7.1.

Give this a try:

import pyodbc
cnxn = pyodbc.connect(
    "Driver={freeTDS};Server=*servername*;DATABASE=*databasename*;"
    "UID=*userid*;pwd=*pwd*;TDS_Version=7.3;CHARSET=UTF-8;"
)

cursor = cnxn.cursor()
cursor.execute("Select * from TestList_Table")
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I'm using version 4.2. This actually resolved a number of the problems I was seeing. Do you know what these changes to the connection string did? I guess I'm wondering from an academic sense.
There are two version numbers to consider. There's the FreeTDS version (which will be very likely be something between 0.91 and 1.1.24), and the TDS Version (which will be 4.2, 5.0, or for SQL Server, 7.0 through 7.4). The FreeTDS version is the number of the FreeTDS driver library, whereas the TDS Version is the version of the protocol defined by Microsoft for SQL Server. It is very confusing! By using TDS_Version 7.3, we're saying "use a recent Microsoft supported version", and by passing the CHARSET as an option, we're saying to use UTF-8.
I've found it is best to be explicit in the connection string with pyodbc rather than mucking about with freetds.conf or odbc.ini; it keeps all the fiddly bits in one place instead of several. Here's the documentation on TDS Version (partially written by yours truly after I figured out this mess myself!): freetds.org/userguide/choosingtdsprotocol.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.