1

I have two .py files

File_1 contains the following statements:

try:
        conStr4SQLserver = pypyodbc.connect('DRIVER={SQL Server};'
                                            'SERVER=........'
                                            'DATABASE=......'
                                            'UID=......;PWD=......')
    except:
        print("I am unable to connect to the SQL SERVER Database")

File_2 contains the following statements:

import AnotherPythonFile as FindClosest

def query(queryStr,fetchOption = 'GetAll'):     # Default value of the parameter fetchOption is 'GetAll' 

    global sqlException
    sqlException = False
    try:
        connection = FindClosest.conStr4SQLserver
        cursr = connection.cursor()

        cursr.execute(queryStr)
        if fetchOption == 'GetOne':
            rows = cursr.fetchone()
        else: 
            rows = cursr.fetchall()

        return rows
    except:
        sqlException = True
        print("Error: Exception Ocured in Function 'query' :", sys.exc_info()[0])

Here's how I call the 'query' function: firstrow= query(queryStr, 'GetOne')

However, in case of an exception, I need to call the query function again.
I recall the function in the following way:

while (sqlException):
        firstrow= query(queryStr, 'GetOne') 

Unfortunately, I keep on getting the <class 'pypyodbc.DatabaseError'> each time the loop executes.

Can you please tell me where the problem is?

Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80
Newbie
  • 95
  • 9

0 Answers0