无法在连接pymysql上捕获异常

时间:2016-08-12 14:46:22

标签: python exception-handling 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:,只是为了测试我是否捕获了错误的错误,没有骰子。

1 个答案:

答案 0 :(得分:1)

在调用函数之前尝试将pymysql.cursors.DictCursor分配给变量:

try:
    cursor = pymysql.cursors.DictCursor
    connection = pymysql.connect(host='localhost',
                                 user=uid,
                                 password=pw,
                                 db='sw_config',
                                 port=5002,
                                 cursorclass=cursor)