简体   繁体   English

运行时使用 SQL 时出现浮点异常,但在调试时没有

[英]Floating point exception when using SQL when run but not when debugged

As part of my Python program, I have created a method which runs sql queries on a Db2 server.作为我的 Python 程序的一部分,我创建了一个在 Db2 服务器上运行 sql 查询的方法。 Here it is:这里是:

def run_query(c, query, return_results=False):
    stmt = db.exec_immediate(c, query)

    if return_results:
        df = {}
        row = db.fetch_assoc(stmt)

        for key in [key.lower() for key in row.keys()]:
            df[key] = []

        while row:
            for key in [key .lower() for key in row.keys()]:
                df[key].append(row[key.upper()])

            row = db.fetch_assoc(stmt)

        return pd.DataFrame(df)

It uses the ibm_db API library and its goal is to run an SQL query.它使用 ibm_db API 库,其目标是运行 SQL 查询。 If the results are wanted, it converts the resultset into a pandas dataframe for use in the program.如果需要结果,它将结果集转换为 pandas dataframe 以在程序中使用。 When I run the program to print out the returned dataframe with print(run_query(conn, "SELECT * FROM ppt_products;", True)) , it does not print anything but instead exits with this error code: Process finished with exit code 136 (interrupted by signal 8: SIGFPE) (btw I am using PyCharm Professional).当我运行程序以print(run_query(conn, "SELECT * FROM ppt_products;", True))返回的 dataframe 时,它不打印任何内容,而是以以下错误代码退出: Process finished with exit code 136 (interrupted by signal 8: SIGFPE) (顺便说一句,我使用的是 PyCharm Professional)。 However, when I debug the program with pydev debugger in PyCharm, the program runs smoothly and prints out the desired output, which should look like:但是,当我在 PyCharm 中使用 pydev 调试器调试程序时,程序运行平稳并打印出所需的 output,它应该如下所示:

         id brand model           url
0      2392   sdf  rtsg  asdfasdfasdf
1  23452345   sdf  rtsg  asdfasdfasdf
2      6245   sdf  rtsg  asdfasdfasdf
3      8467   sdf  rtsg  asdfasdfasdf

I had tried debugging the floating-point-exception but could only find solutions for Python 2 with a module called fpectl which can be used to turn on and off floating-point-exceptions.我曾尝试调试浮点异常,但只能使用名为fpectl的模块找到 Python 2 的解决方案,该模块可用于打开和关闭浮点异常。

I would appreciate any assistance.我将不胜感激任何帮助。

The error was only occurring in PyCharm.该错误仅发生在 PyCharm 中。 When I run it using the command line, the error did not occur.当我使用命令行运行它时,没有发生错误。 This leads me to believe that the error may have been in the JetBrains mechanism for running scripts.这让我相信错误可能出在 JetBrains 运行脚本的机制中。 Thank you data_henrik for the suggestion to use pandas.read_sql because it simplified the process of getting the result set from the SQL queries.感谢 data_henrik 提出使用 pandas.read_sql 的建议,因为它简化了从 SQL 查询中获取结果集的过程。

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

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