pythonsqlpandasdb2python-db-api

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. 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. If the results are wanted, it converts the resultset into a pandas dataframe for use in the program. 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). 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:

         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.

I would appreciate any assistance.


Solution

  • The error was only occurring in 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. 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.