将变量传递给函数时出现Python溢出错误

时间:2017-05-12 10:28:52

标签: python sql-server windows-7-x64 pyodbc

我有一个从数据库中获取任意行数的查询,但通常不超过两行。

我用

调用查询
ret = self.workaround.delete_last_version(pdf_file, self.cust_id, self.part_id)

如果我对值进行硬编码并将它们输入到查询中,它可以正常工作,但是当我正常运行整个程序并通过gui输入数据时,我会在调用查询函数时得到

OverflowError: long too big to convert

但是,在调用之前,我将part_id和cust_id转换为str()

的字符串

以下是我一直在测试的值。

part_id = "168440901713431956015724879141"
cust_id = "15424322074155018763160235136213"
pdf_file = "1971-48.pdf"

cust_id长度为32个字符,part_id为30个

这是查询。

query = """
    SELECT Id, Name, DocDateStamp, Dir_Id
    FROM SdmDocumentList
    WHERE Owner_Id = ? AND Name = ?
    ORDER BY DocDateStamp desc
    """

    self.cursor.execute(query, (part_id, pdf_file))
    result = self.cursor.fetchall()

光标在__init__

中定义
def __init__(self):

    self.conn =pyodbc.connect('DRIVER=SQL Server;SERVER=mssql;PORT=CORRECTPORT;DATABASE=DATABASENAME;UID=USER;PWD=SUPERSECRETPASSWORD;TDS_Version=8.0;Trusted_Connection=no') #windows
    self.cursor = self.conn.cursor()

我从命令行调用函数时得到的预期输出

[('70D8B606A5BD421F97467F2C8D1D8F04', '1971-48.pdf', datetime.datetime(2017, 5, 12, 9, 28, 58, 743333), 9), 
 ('F049665629814B83B63B0536F985090B', '1971-48.pdf', datetime.datetime(2017, 5, 12, 9, 28, 22, 86666), 9), 
 ('39EAB6B173B745E19BA8A0598AD8F015', '1971-48.pdf', datetime.datetime(2017, 5, 12, 9, 27, 58, 933333), 9), 
 ('6309915CEA504839A8D7340F9A4FD601', '1971-48.pdf', datetime.datetime(2017, 5, 12, 9, 12, 26, 36666), 9), 
 ('5D3E3AA218CA4FF59E8EC2DE2A6AB217', '1971-48.pdf', datetime.datetime(2017, 5, 12, 9, 11, 37, 760000), 9), 
 ('1C2AC1073E754A41A998DF99BD0F4F48', '1971-48.pdf', datetime.datetime(2017, 5, 12, 9, 10, 49, 986666), 9), 
 ('05EF8020EA354E669D1D930650FBCB02', '1971-48.pdf', datetime.datetime(2017, 5, 12, 8, 54, 59, 80000), 9), 
 ('834979EFB639466ABC73E76F88DF6750', '1971-48.pdf', datetime.datetime(2017, 5, 12, 8, 54, 5, 46666), 9), 
 ('F3EF2C75856E4926A52204EBA59072CB', '1971-48.pdf', datetime.datetime(2017, 5, 12, 8, 50, 38, 406666), 9),
 ('6F7FC2652E114162AF6B3C4C30818582', '1971-48.pdf', datetime.datetime(2017, 5, 12, 8, 37, 59, 610000), 9)]

我在Windows7 64位和6 GB的RAM上运行Python2.7,使用mssql

我用Google搜索并查看了OverflowErrors的其他问题,并且很长时间无法转换,但是发布的所有解决方案都没有解决我的问题,而且我在互联网上找到的所有问题都很早就被转换为另一种数字类型。在我得到错误的行,我甚至没有尝试转换任何东西,只是将变量传递给函数。

是否有一些内存限制可以在Python中向函数发送多少数据,或者我是否完全错过了其他内容?

如果需要,我很乐意提供更多信息,但我想我已经写下了为此所需的一切。

提前感谢您提供任何帮助

更新pyodbc后失败的EDIT查询:

更新pyodbc后导致问题的新查询

 Dir_Id = 9                              # Manufactering Data folder (from SdmDirSructure)
        DocumentType_RecNo = 3                  # PDF
        OwnerType_RecNo = 2                     # parent_id for Product (from SdmDirSructure)
        FromTo = 14872094126171117726173141183125     # CNCDrill user ID
        DocumentSubject_RecNo = 109             # Xcheck data (from SdmDocumentSubjects)
        Owner_Id = self.part_id                 # It is the part id
        DocDateStamp = file_datetime            # datetime of the document
        RegisteredDate = file_datetime          # when the document was added to the doc manager
        LastReadDate = file_datetime            # date that the document last readed (set as the RegisteredDate)
        ArchiveLocation_RecNo = 0               # Not in use
        IsReceived = True                       # Not in use
        IsArchived = False                      # Not in use
        IsOwnedByUser = 0                       # No-one ownes the file

        query = """
        INSERT INTO SdmDocumentList (Id, Name, Dir_Id, DocumentType_RecNo, OwnerType_RecNo, FromTo, DocumentSubject_RecNo, Owner_Id, DocDateStamp, RegisteredDate, LastReadDate, ArchiveLocation_RecNo, IsReceived, IsArchived, IsOwnedByUser, IsClassified) 
        VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
        """
        self.cursor.execute(query, (uid,pdf_file, Dir_Id, DocumentType_RecNo, OwnerType_RecNo, FromTo, DocumentSubject_RecNo,Owner_Id, DocDateStamp, RegisteredDate, LastReadDate, ArchiveLocation_RecNo, IsReceived, IsArchived, IsOwnedByUser, IsClassified))
        self.cursor.commit()

1 个答案:

答案 0 :(得分:2)

 FromTo = 14872094126171117726173141183125     # CNCDrill user ID

杯子。最后我开始打印每个变量及其类型。事实证明,我在哪里硬编码了“FromTo'变量,对于这个剧本来说,它永远不会改变,我忘记了把#34; "围绕它,使它变得很长,这使得查询崩溃。

我意识到当我在脚本中早些时候将部件和客户ID从数据库中取出时,它将它们作为Unicode返回。我做了一些关于Unicode的研究,并尝试了替代方案,例如在脚本顶部定义编码,但它们没有用。然而,这导致我检查每个变量是我期望的类型的正确路径。

感谢周五的所有帮助@Gord Thompson May