Python 无法捕获连接pymysql上的异常 @classmethod def getSwDataPass(self、uid、pw、dbname='swdata',_count=0): 结果=无 如果dbname='': dbname=input('找不到名为swdata的数据库。请提供数据库名称:') 如果dbname=''或dbname为无: 打印('连接到数据库时出现问题…正在退出') sys.exit() 尝试: connection=pymysql.connect(host='localhost', user=uid, 密码=pw, db='sw_config', 端口=5002, cursorclass=pymysql.cursors.DictCursor) 除了pymysql.err.error之外: print('无法连接到mysql数据库。请提供您的swdata凭据') 凭据=self.getrootpass(True) 结果={“uid”:凭据[0],“pwd”:凭据[1]} 持续 如果结果为无: 尝试: 使用connection.cursor()作为游标: execute(“从sw_数据库中选择uid、pwd、dbname,其中dbname='{0}'”。格式(dbname)) 结果=cursor.fetchone() 如果结果为无: 连接。关闭() 如果_计数小于2: self.getSwDataPass(uid,pw,,_count+1) 最后: 连接。关闭() 返回结果

Python 无法捕获连接pymysql上的异常 @classmethod def getSwDataPass(self、uid、pw、dbname='swdata',_count=0): 结果=无 如果dbname='': dbname=input('找不到名为swdata的数据库。请提供数据库名称:') 如果dbname=''或dbname为无: 打印('连接到数据库时出现问题…正在退出') sys.exit() 尝试: connection=pymysql.connect(host='localhost', user=uid, 密码=pw, db='sw_config', 端口=5002, cursorclass=pymysql.cursors.DictCursor) 除了pymysql.err.error之外: print('无法连接到mysql数据库。请提供您的swdata凭据') 凭据=self.getrootpass(True) 结果={“uid”:凭据[0],“pwd”:凭据[1]} 持续 如果结果为无: 尝试: 使用connection.cursor()作为游标: execute(“从sw_数据库中选择uid、pwd、dbname,其中dbname='{0}'”。格式(dbname)) 结果=cursor.fetchone() 如果结果为无: 连接。关闭() 如果_计数小于2: self.getSwDataPass(uid,pw,,_count+1) 最后: 连接。关闭() 返回结果,python,exception-handling,pymysql,Python,Exception Handling,Pymysql,当我输入连接到mysql的无效凭据时,我会得到一个未处理的异常,程序将以错误退出 @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 databa

当我输入连接到mysql的无效凭据时,我会得到一个未处理的异常,程序将以错误退出

@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
回溯(最近一次呼叫最后一次):
文件“buildzapp.py”,第248行,在
BuildZapp.main()
文件“buildzapp.py”,第96行,在main中
zapp=BuildZapp(manifest,pw,uid,dbname)
文件“buildzapp.py”,第51行,在_init中__
swdata\u credentials=Utilities.getSwDataPass(self.conf\u uid,self.conf\u pw)
文件“C:\zapp builder\utilities.py”,第60行,在getSwDataPass中
cursorclass=pymysql.cursors.DictCursor)
文件“C:\Python27\Lib\site packages\pymysql\\uuuu init\uuuuu.py”,第88行,在Connect中
返回连接(*args,**kwargs)
文件“C:\Python27\Lib\site packages\pymysql\connections.py”,第689行,在\uuu init中__
self.connect()
文件“C:\Python27\Lib\site packages\pymysql\connections.py”,第907行,在connect中
self.\u请求\u身份验证()
文件“C:\Python27\Lib\site packages\pymysql\connections.py”,第1115行,在请求认证中
auth_packet=self.\u read_packet()
文件“C:\Python27\Lib\site packages\pymysql\connections.py”,第982行,在_read_数据包中
packet.check_error()
文件“C:\Python27\Lib\site packages\pymysql\connections.py”,第394行,检查错误
err.raise\u mysql\u异常(self.\u数据)
raise\U mysql\U异常中的文件“C:\Python27\Lib\site packages\pymysql\err.py”,第120行
_检查\u mysql\u异常(errinfo)
文件“C:\Python27\Lib\site packages\pymysql\err.py”,第112行,在\u check\u mysql\u异常中
提高errorclass(错误号,错误值)
pymysql.err.OperationalError:(1045,u“拒绝用户'root'@'localhost'的访问”(使用密码:YES)”

我只尝试了一个打开的
,除了:
只是为了测试我是否捕捉到了错误,没有骰子。

在调用函数之前,尝试将
pymysql.cursors.DictCursor
赋值给变量:

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)")

这是关键。我想知道为什么,你能提供这种行为的原因吗?@richbai90,它可能是一个bug,也可能与代码的使用方式有关。这我不知道,但如果您想了解原因,您可能应该询问模块开发人员。
try:
    cursor = pymysql.cursors.DictCursor
    connection = pymysql.connect(host='localhost',
                                 user=uid,
                                 password=pw,
                                 db='sw_config',
                                 port=5002,
                                 cursorclass=cursor)