简体   繁体   中英

Retrieving records from SQL Server with PYODBC and FreeTDS

I have a python script that connects to a local SQL Server (2012, i believe?) and runs a "SELECT * from ..." query. When I run the script, I get an error saying:

File "/home/mdrouin/dev/redbus/wyndham.py", line 643, in connect

cursor.execute("SELECT * from [RentalDB].[dbo].[Clients]")

pyodbc.ProgrammingError: ('42000', '[42000] [FreeTDS][SQL Server]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. (4004) (SQLExecDirectW)')

After reading some other posts, I tried a suggestion another thread mentioned by adding the following to my script:

os.environ['TDSVER'] = '7.0'

However, when I do this, I get unicode escape strings (I think that's what they're called...) returned, instead of the data. This is an example of when I print a row from the table:

(u'\U00320035\U00380033\U00360033\U00300030\U00300030\U00300030\U00320031\U00320039\U00350039', u'\U00640041\U006d0061\U00520020\U004d0020\U00740061\U00690074\U0067006e\U0079006c\U00280020\U00590057\U0032004e\U00300031', u'\U00640061\U006d0061\U00390073\U006d006d', u'\U006f006a\U006e0068\U00650064\U00720065\U00390065\U00330034', u'\U00330032\U0031002e\U002e0039\U00310035\U0031002e\U00350031')

The thing I don't get is that I am running this on a VM that is set up exactly the same as another VM, and one of the VMs runs fine while the other doesn't. The other VM doesn't have either of these issues. I checked the odbc.ini and odbcinst.ini files on both computers, and they are exactly the same, so I don't know what could be causing the issues. Both VMs are running Debian 7.

Nevermind... I'm retarded. When I set up the VirtualEnv in Python on the VM that wasn't working, I accidentally used Python 2.6 rather than Python 2.7, which in turn only let PYODBC 2.x be installed rather than PYODBC 3.x. The older version of PYODBC was causing the issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM