Python truncate at 255 chars when querying MS SqlS

2020-04-16 19:04发布

问题:

i think i have miss something in a query from python using pyodbc over a FreeTDS on a Debian machine. I can not manage to have more than the first 255 characters.

if i try to CAST SQL data as describe here: ODBC query on MS SQL Server returning first 255 characters only in PHP PDO (FreeTDS) with : SELECT CAST(myText as TEXT) FROM mytable i get empty/zero length string,

i have also test with no more success to : change size as describe with the config below, i only get first 255 characters (while on my Win box pyodbc can get 279 characters, for example).

here is a sample query :

SET TEXTSIZE 2147483647;
SELECT [id], myText , LEN(myText)
FROM    mytable

here is my /etc/freetds/freetds/conf: (what is the meaning of the ; @ beginning of line ?)

[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

the others conf files are:

> 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=

my SQL server is on 192.168.1.2, and i connect by using :

cnstr = 'DRIVER={FreeTDS};SERVER=192.168.1.2;DATABASE=MyDBName;UID=MyUID;PWD=MYPASSWD'
cursor = cnxn.cursor()
cursor.execute(SQL)
# etc.

thank u for any help,

best regards

回答1:

what is the meaning of the ; @ beginning of line?

It starts a comment. man freetds.conf is your friend. :-)

I can not manage to have more than the first 255 characters.

I suspect you are using protocol version 4.2, because you're using a DSN-less connection and did not mention setting the default. Also because TDS 4.2 limits VARCHAR columns to 255 characters.

You can add the protocol version to your connection string, cf. http://www.freetds.org/userguide/odbcconnattr.htm.