我想我在 Debian 机器上使用 pyodbc 通过 FreeTDS 从 python 进行的查询中遗漏了一些内容。我的字符数不能超过前 255 个。
如果我尝试按此处所述转换 SQL 数据:ODBC query on MS SQL Server returning first 255 characters only in PHP PDO (FreeTDS)和 :
从 mytable 中选择 CAST(myText as TEXT)
我得到空/零长度字符串,
我还进行了测试,但没有取得更多成功:按照下面的配置描述更改大小,我只得到前 255 个字符(例如,在我的 Win 盒子上 pyodbc 可以得到 279 个字符)。
这是一个示例查询:
SET TEXTSIZE 2147483647;
SELECT [id], myText , LEN(myText)
FROM mytable
这是我的/etc/freetds/freetds/conf
:
(行首 ; @ 的含义是什么?)
[global]
# TDS protocol version
; tds version = 4.2
#!!!added by me
client charset = UTF-8
text size = 4294967295
#!!!/added by me
# 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
#!!!changed by me
#text size = 64512
其他conf文件是:
> cat /etc/odbc.ini #/etc/odbc.ini is empty! is it ok?
> cat /etc/odbcinst.ini
[FreeTDS]
Description=TDS driver (Sybase/MS SQL)
Driver=/usr/lib/odbc/libtdsodbc.so
Setup=/usr/lib/odbc/libtdsS.so
CPTimeout=
CPReuse=
我的 SQL 服务器位于 192.168.1.2
,我使用以下方式进行连接:
cnstr = 'DRIVER={FreeTDS};SERVER=192.168.1.2;DATABASE=MyDBName;UID=MyUID;PWD=MYPASSWD'
cursor = cnxn.cursor()
cursor.execute(SQL)
# etc.
感谢您的帮助,
最诚挚的问候
最佳答案
what is the meaning of the ; @ beginning of line?
它开始一条评论。 man freetds.conf 是你的 friend 。 :-)
I can not manage to have more than the first 255 characters.
我怀疑您使用的是协议(protocol)版本 4.2,因为您使用的是无 DSN 连接并且没有提及设置默认值。还因为 TDS 4.2 将 VARCHAR 列限制为 255 个字符。
您可以将协议(protocol)版本添加到连接字符串中,参见。 http://www.freetds.org/userguide/odbcconnattr.htm 。
关于使用 FreeTDS 查询 MS SqlServer 时,Python 在 255 个字符处截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13381854/