0

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.

rhobro
  • 103
  • 1
  • 7
  • Your problem asside, why are you not using something like read_sql? https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql.html – data_henrik May 08 '20 at 05:46
  • Try to narrow down the problem. Run the code outside of Pycharm , using the python at the operating-system-shell to compare the behaviour (even if the python version differs). Add some defensive code to check that each ibm_db function returns something valid otherwise exit or return None. Only do the print action when there is a dataframe returned etc. – mao May 08 '20 at 09:32
  • I am going to try and use pandas.read_sql. Thanks mao. I shall try your suggestions and get back to you. – rhobro May 08 '20 at 12:07
  • I am using pd.read_sql which has reduced the amount of code I need. Now I am trying to stop the fpe – rhobro May 08 '20 at 15:58
  • Once again, narrow down the issue. EDIT your question to show your current code, and to show your version of python , ouput of `pip freeze` and whether you get the FPE outside of PyCharm. It is up to you to do the troubleshooting as only you have your configuration. – mao May 09 '20 at 12:11

1 Answers1

0

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.

rhobro
  • 103
  • 1
  • 7