我有两个 .py 文件
try:
conStr4SQLserver = pypyodbc.connect('DRIVER={SQL Server};'
'SERVER=........'
'DATABASE=......'
'UID=......;PWD=......')
except:
print("I am unable to connect to the SQL SERVER Database")
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])
以下是我调用'query'函数的方法:firstrow= query(queryStr, 'GetOne')
但是,如果发生异常,我需要再次调用查询函数。
我用以下方式回忆起这个功能:
while (sqlException):
firstrow= query(queryStr, 'GetOne')
不幸的是,每次循环执行时我都会继续<class 'pypyodbc.DatabaseError'>
。
你能告诉我问题出在哪里吗?