我在Debian Lenny(5.0.7)上成功使用pyodbc时遇到了一些麻烦。具体来说,我似乎无法获取NVARCHAR值(不是SQL Server专家,所以对我很容易:) :)。
大多数传统查询都可以。例如,table1中的行数产生
cursor.execute("SELECT count(id) from table1")
<pyodbc.Cursor object at 0xb7b9b170>
>>> cursor.fetchall()
[(27, )]
完全转储ID
>>> cursor.execute("SELECT id FROM table1")
<pyodbc.Cursor object at 0xb7b9b170>
>>> cursor.fetchall()
[(0.0, ), (3.0, ), (4.0, ), (5.0, ), (6.0, ), (7.0, ), (8.0, ), (11.0, ), (12.0, ), (18.0, ), (19.0, ), (20.0, ), (21.0, ), (22.0, ), (23.0, ), (24.0, ), (25.0, ), (26.0, ), (27.0, ), (28.0, ), (29.0, ), (32.0, ), (33.0, ), (34.0, ), (35.0, ), (36.0, ), (37.0, )]
但是名称转储(同样是NVARCHAR类型)不是
>>> cursor.execute("SELECT name FROM table1")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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)')
......关键错误
pyodbc.ProgrammingError:('42000','[42000] [FreeTDS] [SQL Server]仅使用Unicode的归类或ntext数据中的Unicode数据无法使用DB-Library(例如ISQL)发送到客户端)或ODBC 3.7或更早版本。(4004)(SQLExecDirectW)')
这在表格中是一致的。
我尝试了各自的各种不同版本,但现在我正在运行unixODBC 2.2.11(来自lenny repos),FreeTDS 0.91(源自./configure --enable-msdblib --with-tdsver=8.0
)和pyodbc 3.0。 3(从源头构建)。
使用类似的组合(unixODBC 2.3.0,FreeTDS 0.91,pyodbc 3.0.3),相同的代码适用于Mac OS X 10.7.2。
我搜索了高低,调查了here和here提供的解决方案,并重新编译了unixODBC和FreeTDS的不同版本,但仍然没有骰子。下面提供的相关配置文件:
user@host:~$ cat /usr/local/etc/freetds.conf
#$Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".
# Global settings are overridden by those in a database
# server specific section
[global]
# TDS protocol version
tds version = 8.0
client charset = UTF-8
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# A typical Sybase server
[egServer50]
host = symachine.domain.com
port = 5000
tds version = 5.0
# A typical Microsoft server
[egServer70]
host = ntmachine.domain.com
port = 1433
tds version = 8.0
[foo]
host = foo.bar.com
port = 1433
tds version = 8.0
user@host:~$ cat /etc/odbc.ini
[foo]
Description = Foo
Driver = foobar
Trace = No
Database = db
Server = foo.bar.com
Port = 1433
TDS_Version = 8.0
user@host:~$ cat /etc/odbcinst.ini
[foobar]
Description = Description
Driver = /usr/lib/odbc/libtdsodbc.so
Setup = /usr/lib/odbc/libtdsS.so
CPTimeout =
CPReuse =
非常感谢任何建议或指示!
答案 0 :(得分:10)
我遇到了与Ubuntu相同的错误。我解决了#34;它有一个解决方法。 您需要做的就是设置环境变量 TDSVER 。
import os
os.environ['TDSVER'] = '8.0'
正如我所说,这不是一个真正的解决方案&#34;但它确实有效。
答案 1 :(得分:6)
尝试添加
TDS_Version=8.0;ClientCharset=UTF-8
在你的连接字符串中。
例如,
DRIVER=FreeTDS;SERVER=myserver;DATABASE=mydatebase;UID=me;PWD=pwd;TDS_Version=8.0;ClientCharset=UTF-8
答案 2 :(得分:1)
你是不是只想解决这个问题并将转换或转换name
转换为可以处理的问题?
cursor.execute(“SELECT CAST(name AS TEXT)FROM FROM”)