Can't get full data from SQL Server with python

Question:

I’m trying to get query as a xml data from mssql server with pyodbc. After query, im writing data to new xml file with unique name. Everything works fine to this point with small data. Problem is when i try to read data over 2037 character, i cant get all of them. Its gives me just first 2037 character.

SQL Server version 15.0.2000.5(SQL Server 2019 Express)

Driver is ODBC Driver 17 for SQL Server

Python version 3.11.1

pyodbc version 4.0.35

Code is running on Windows Server 2016 Standard

SQL Query For XML Data

SELECT 
  C.BLKODU AS "BLKODU", 
  C.CARIKODU AS "CARIKODU", 
  C.TICARI_UNVANI AS "TICARI_UNVANI", 
  C.ADI_SOYADI AS "ADI_SOYADI", 
  C.VERGI_NO AS "VERGI_NO", 
  C.TC_KIMLIK_NO AS "TC_KIMLIK_NO", 
  C.VERGI_DAIRESI AS "VERGI_DAIRESI", 
  C.CEP_TEL AS "CEP_TEL", 
  C.ILI AS "ILI", 
  C.ILCESI AS "ILCESI", 
  C.ADRESI_1 AS "ADRESI", 
  (SELECT 
      CHR.BLKODU AS "BLKODU", 
      CHR.EVRAK_NO AS "EVRAK_NO", 
      CHR.MAKBUZNO AS "MAKBUZ_NO", 
      CAST(CHR.TARIHI AS DATE) AS "TARIHI", 
      CAST(CHR.VADESI AS DATE) AS "VADESI",  
      CHR.MUH_DURUM AS "MUH_DURUM", 
      CAST(CHR.KPB_ATUT AS DECIMAL(10, 2)) AS "KPB_ATUT", 
      CAST(CHR.KPB_BTUT AS DECIMAL(10, 2)) AS "KPB_BTUT" 
   FROM CARIHR AS CHR 
   WHERE CHR.BLCRKODU = C.BLKODU 
   ORDER BY CHR.TARIHI
   FOR XML PATH('CARIHR'), TYPE) 
FROM CARI AS C 
WHERE C.CARIKODU = 'CR00001'
FOR XML PATH ('CARI')

Python Code

import pyodbc
import uuid
import codecs

import query
import core


conn = pyodbc.connect(core.connection_string, commit=True)
cursor = conn.cursor()
cursor.execute(query.ctr)
row = cursor.fetchval()
id = uuid.uuid4()
xml_file = "./temp/"+str(id)+".xml"
xml = codecs.open(xml_file, "w", "utf-8")
xml.write(row)
xml.close()

I’ve tried to use pymssql and it didn’t change anything.

cursor.fetchvall(), cursor.fetchone() is gives me same result.

cursor.fetchall() gives me full data. But its gives as a list. When its gives as a list i need to convert to string. Before converting to string i need to select first element in the list. So i came with then idea like this below. But result didn’t change at all. Its still gives only first 2037 character.

conn = pyodbc.connect(connect_string, commit=True)
cursor = conn.cursor()
cursor.execute(query.ctr)
row = cursor.fetchall()
data = ','.join(row[0])
id = uuid.uuid4()
xml_file = "./temp/"+str(id)+".xml"
xml = codecs.open(xml_file, "w", "utf-8")
xml.write(data)
xml.close()
Asked By: exeen

||

Answers:

For XML queries are split to multiple lines by SQL Server automatically if they’re long enough. Some clients like Management Studio "merge" these to single row but it’s not actually one row.
So you need to concatenate your string yourself:

#code in pseudo-python
xmlString = ""
rows = cursor.fetchall()
for row in rows:
   xmlString = xmlString + row[0]
Answered By: siggemannen
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.