无法在连接pymysql上捕获异常
unable to catch exception on connection pymysql
@classmethod
def getSwDataPass(self, uid, pw, dbname='swdata', _count=0):
result = None
if dbname == '':
dbname = input('Could not find a database by the name swdata. Please provide the database name : ')
if dbname == '' or dbname is None:
print('There was a problem connecting to the database...exiting')
sys.exit()
try:
connection = pymysql.connect(host='localhost',
user=uid,
password=pw,
db='sw_config',
port=5002,
cursorclass=pymysql.cursors.DictCursor)
except pymysql.err.OperationalError:
print('Unable to make a connection to the mysql database. Please provide your swdata credentials')
credentials = self.getrootpass(True)
result = {"uid": credentials[0], "pwd": credentials[1]}
continue
if result is None:
try:
with connection.cursor() as cursor:
cursor.execute("select uid, pwd, dbname from sw_databases where dbname = '{0}'".format(dbname))
result = cursor.fetchone()
if result is None:
connection.close()
if _count < 2:
self.getSwDataPass(uid, pw, '', _count + 1)
finally:
connection.close()
return result
当我输入无效凭据以连接到 mysql 时,出现未处理的异常并且程序退出并出现错误
Traceback (most recent call last):
File "buildzapp.py", line 248, in <module>
BuildZapp.main()
File "buildzapp.py", line 96, in main
zapp = BuildZapp(manifest, pw, uid, dbname)
File "buildzapp.py", line 51, in __init__
swdata_credentials = Utilities.getSwDataPass(self.conf_uid, self.conf_pw)
File "C:\zapp builder\utilities.py", line 60, in getSwDataPass
cursorclass=pymysql.cursors.DictCursor)
File "C:\Python27\Lib\site-packages\pymysql\__init__.py", line 88, in Connect
return Connection(*args, **kwargs)
File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 689, in __init__
self.connect()
File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 907, in connect
self._request_authentication()
File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 1115, in _request_authentication
auth_packet = self._read_packet()
File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 982, in _read_packet
packet.check_error()
File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 394, in check_error
err.raise_mysql_exception(self._data)
File "C:\Python27\Lib\site-packages\pymysql\err.py", line 120, in raise_mysql_exception
_check_mysql_exception(errinfo)
File "C:\Python27\Lib\site-packages\pymysql\err.py", line 112, in _check_mysql_exception
raise errorclass(errno, errorvalue)
pymysql.err.OperationalError: (1045, u"Access denied for user 'root'@'localhost' (using password: YES)")
我试过一次打开 except:
只是为了测试我是否捕捉到了错误的错误,没有骰子。
尝试在调用函数之前将 pymysql.cursors.DictCursor
赋值给一个变量:
try:
cursor = pymysql.cursors.DictCursor
connection = pymysql.connect(host='localhost',
user=uid,
password=pw,
db='sw_config',
port=5002,
cursorclass=cursor)
@classmethod
def getSwDataPass(self, uid, pw, dbname='swdata', _count=0):
result = None
if dbname == '':
dbname = input('Could not find a database by the name swdata. Please provide the database name : ')
if dbname == '' or dbname is None:
print('There was a problem connecting to the database...exiting')
sys.exit()
try:
connection = pymysql.connect(host='localhost',
user=uid,
password=pw,
db='sw_config',
port=5002,
cursorclass=pymysql.cursors.DictCursor)
except pymysql.err.OperationalError:
print('Unable to make a connection to the mysql database. Please provide your swdata credentials')
credentials = self.getrootpass(True)
result = {"uid": credentials[0], "pwd": credentials[1]}
continue
if result is None:
try:
with connection.cursor() as cursor:
cursor.execute("select uid, pwd, dbname from sw_databases where dbname = '{0}'".format(dbname))
result = cursor.fetchone()
if result is None:
connection.close()
if _count < 2:
self.getSwDataPass(uid, pw, '', _count + 1)
finally:
connection.close()
return result
当我输入无效凭据以连接到 mysql 时,出现未处理的异常并且程序退出并出现错误
Traceback (most recent call last):
File "buildzapp.py", line 248, in <module>
BuildZapp.main()
File "buildzapp.py", line 96, in main
zapp = BuildZapp(manifest, pw, uid, dbname)
File "buildzapp.py", line 51, in __init__
swdata_credentials = Utilities.getSwDataPass(self.conf_uid, self.conf_pw)
File "C:\zapp builder\utilities.py", line 60, in getSwDataPass
cursorclass=pymysql.cursors.DictCursor)
File "C:\Python27\Lib\site-packages\pymysql\__init__.py", line 88, in Connect
return Connection(*args, **kwargs)
File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 689, in __init__
self.connect()
File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 907, in connect
self._request_authentication()
File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 1115, in _request_authentication
auth_packet = self._read_packet()
File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 982, in _read_packet
packet.check_error()
File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 394, in check_error
err.raise_mysql_exception(self._data)
File "C:\Python27\Lib\site-packages\pymysql\err.py", line 120, in raise_mysql_exception
_check_mysql_exception(errinfo)
File "C:\Python27\Lib\site-packages\pymysql\err.py", line 112, in _check_mysql_exception
raise errorclass(errno, errorvalue)
pymysql.err.OperationalError: (1045, u"Access denied for user 'root'@'localhost' (using password: YES)")
我试过一次打开 except:
只是为了测试我是否捕捉到了错误的错误,没有骰子。
尝试在调用函数之前将 pymysql.cursors.DictCursor
赋值给一个变量:
try:
cursor = pymysql.cursors.DictCursor
connection = pymysql.connect(host='localhost',
user=uid,
password=pw,
db='sw_config',
port=5002,
cursorclass=cursor)