使用 FreeTDS 查询 MS SqlServer 时,Python 在 255 个字符处截断

标签 python pyodbc freetds

我想我在 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?

它开始一条评论。 ma​​n 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/

相关文章:

python - 使用 PYODBC 和 FreeTDS 从 SQL Server 检索记录

python - pyodbc的 `Cursor.execute`返回哪个游标对象?

django - pyodbc 在 heroku 服务器上安装错误命令 "gcc"失败,退出状态 1 - Django 项目

Python - Linux - 使用 Windows 凭据连接到 MS SQL - FreeTDS+UnixODBC + pyodbc 或 pymssql

php - 在 PHP PDO-ODBC、unixODBC 和 FreeTDS 中使用类型化绑定(bind)参数

Python Pandas Dataframe - 优化在另一个 Dataframe 中搜索 id

python - 如何更改字典中的元组值?

python - 使用 Python 列出 linux 中的所有守护进程/服务名称?

ruby - 如何在循环中使用 Ruby Sequel 和 Tiny_TDS 保持与 SQL Server 的持久连接

python - 查找每个具有不确定数量的已定义键的所有词典