简体   繁体   English

无法使用 python 从 SQL 服务器获取完整数据

[英]Can't get full data from SQL Server with python

I'm trying to get query as a xml data from mssql server with pyodbc.我正在尝试使用 pyodbc 从 mssql 服务器获取 xml 数据查询。 After query, im writing data to new xml file with unique name.查询后,我将数据写入具有唯一名称的新 xml 文件。 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.问题是当我尝试读取超过 2037 个字符的数据时,我无法获取所有数据。 Its gives me just first 2037 character.它只给了我第一个 2037 个字符。

SQL Server version 15.0.2000.5(SQL Server 2019 Express) SQL 服务器版本15.0.2000.5(SQL Server 2019 Express)

Driver is ODBC Driver 17 for SQL Server驱动程序是ODBC Driver 17 for SQL Server

Python version 3.11.1 Python 版本3.11.1

pyodbc version 4.0.35 pyodbc 版本4.0.35

Code is running on Windows Server 2016 Standard代码在 Windows Server 2016 Standard 上运行

SQL Query For XML Data SQL 查询XML数据

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 Python 代码

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.我试过使用 pymssql 但它没有改变任何东西。

cursor.fetchvall(), cursor.fetchone() is gives me same result. cursor.fetchvall()、cursor.fetchone() 给了我相同的结果。

cursor.fetchall() gives me full data. cursor.fetchall() 给了我完整的数据。 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.在转换为字符串之前,我需要 select 列表中的第一个元素。 So i came with then idea like this below.所以我想出了下面这样的想法。 But result didn't change at all.但结果根本没有改变。 Its still gives only first 2037 character.它仍然只给出第一个 2037 个字符。

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()

For XML queries are splitted to multiple lines by SQL Server automatically if they're long enough.对于 XML,如果查询足够长,则 SQL 服务器会自动将其拆分为多行。 Some clients like Management Studio automatically "merge" these to single row but it's not actually one row. Management Studio 等一些客户端会自动将这些“合并”到单行,但实际上并不是一行。 So you need to contanate your string yourself:所以你需要自己连接你的字符串:

#code in pseudo-python
xmlString = ""
rows = cursor.fetchall()
for row in rows:
   xmlString = xmlString + row[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM