1
@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我输入凭据无效,我得到一个未处理的异常,并且程序用一个错误无法赶上连接pymysql
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:
退出只是为了测试我是否抓住了错误的错误,没有骰子。
这是关键。我想知道为什么,你能否提供有关导致此行为的洞察? – richbai90
@ richbai90,它可能是一个错误,或者它可能与代码的使用方式有关。我不知道,但如果你想了解原因,你应该问模块开发人员。 – Flippym