简体   繁体   中英

pymssql issue with cutting off after 255 characters

I have installed pymssql (using _mssql) version 1.0.2 and I noticed that when I start looking at the fields in the result set it appears to be cutting off large text fields at 255 characters max. I scoured the internet for any mention of this but found nothing - I have no idea why it is cutting the string off at this point, but the database is definitely returning a longer string than that. Any help is appreciated - could it just be as easy as a version upgrade, or is this something I need to work around?

Thanks!

As per your request, I am inserting an obscured code snippet detailing what I am talking about - this is a Python script on an Ubuntu system:

import sys
import shutil
import fileinput
import os
import _mssql

myServer = 'hostIP'
myUID = 'userID'
myPW = 'mypass'
myDB = 'theDBName'
theID  = 'ABC123'

myConn = _mssql.connect(server=myServer, user=myUID, password=myPW, database=myDB)
myConn.execute_query("SELECT method FROM myTable WHERE id = '" + theID + "'")

for myRow in myConn:
  method = myRow["method"]
  len(method) # This prints out 255
  len(myRow["method")) # This prints out 255

myConn.close()

FYI : the string returned from the database is over 900 characters in length, but I only see the first 255 of it.

This appears to be a known issue with pymssql (or rather, the underlying protocol).

varchar and nvarchar data is limited to 255 characters, and longer strings are silently trimmed. This is known limitation of TDS protocol. A workaround is to CAST or CONVERT that row or expression to text data type, which is capable of returning 4000 characters.

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