简体   繁体   English

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

[英]Python Overflow Error when passing variables to function

I have a query that gets an arbitary number of rows from the database, but usually no more than two. 我有一个查询,该查询从数据库中获取任意数量的行,但通常不超过两行。

I call the query with 我用

ret = self.workaround.delete_last_version(pdf_file, self.cust_id, self.part_id)

If i hardcode the values and feed them into the query, it works fine, but when I run the whole program normally, and input the data in through a gui, I get when the query function is called 如果我对值进行硬编码并将其输入查询中,则可以正常工作,但是当我正常运行整个程序并通过gui输入数据时,调用查询函数时会得到

OverflowError: long too big to convert

However, before the call is made, I convert part_id and cust_id to strings with str() 但是,在进行调用之前,我使用str()将part_id和cust_id转换为字符串

Here are the values that I've been testing with. 这是我一直在测试的值。

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

The cust_id is 32 chars long, and part_id is 30 cust_id为32个字符,part_id为30

And here is the query. 这是查询。

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

And the cursor is defined in __init__ 游标在__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()

The expected output, which I get when I call the function from command line 从命令行调用函数时得到的预期输出

[('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)]

I'm running Python2.7 on Windows7 64bit and 6gb of RAM, with mssql 我在Windows7 64位和6GB RAM上运行Python2.7,使用mssql

I've Googled and looked at other issues with OverflowErrors and longs being too long to convert, but none of the solutions posted seemed to fix my issue, and all the issues I found on the internet had long being converted to another numeric type. 我已经用Google搜索并查看了OverflowErrors的其他问题,并且转换的时间太长了,但是发布的所有解决方案似乎都无法解决我的问题,而且我在互联网上发现的所有问题都已长期转换为其他数字类型。 At the line I get the error, I'm not even trying to convert anything, just pass the variables to a function. 在发生错误的那一行,我什至没有尝试转换任何东西,只是将变量传递给函数。

Is there some memory limit to how much data can be sent to a function in Python, or am I missing something else entirely? 是否可以在Python中向函数发送多少数据有一定的内存限制,还是我完全丢失了其他东西?

More than happy to provide more information if needed, but I think I've written down everything that would be needed for this. 很乐意在需要时提供更多信息,但是我想我已经写下了所有需要的信息。

Thanks in advance for any help 预先感谢您的任何帮助

EDIT Query that fails after updating pyodbc: 更新pyodbc后,编辑查询失败:

The new query that's causing problems after updating pyodbc 更新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()
 FromTo = 14872094126171117726173141183125     # CNCDrill user ID

The cuplrit. 铜杯。 Eventually I started printing every variable and its type. 最终,我开始打印每个变量及其类型。 It turns out that where I'd hardcoded the 'FromTo' variable, which wasn't going to change, ever, for this script, I had forgotten to put " " around it, making it a long, which was crashing the query. 事实证明,对于该脚本,我从未对其进行硬编码的“ FromTo”变量进行过更改,而我却忘记在其周围加上“”,使其变长了,这使查询崩溃了。

I'd realised that when I was getting the part and customer id's out of the database earlier in the script, it was returning them as Unicode. 我意识到,当我在脚本的较早部分将零件和客户ID从数据库中取出时,它会将它们作为Unicode返回。 I did some research into Unicode, and tried alternatives, such as defining the encoding at the top of the script, but they didn't work. 我对Unicode进行了一些研究,并尝试了其他方法,例如在脚本顶部定义编码,但是它们不起作用。 However, this led me down the correct path of checking that every variable was the type I was expecting. 但是,这使我无法正确检查每个变量是否是我期望的类型。

Thanks for all the help on Friday, @Gord Thompson May 感谢星期五的所有帮助,@ Gord Thompson May

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

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