gpt4 book ai didi

python - 无法捕获连接 pymysql 上的异常

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:26 26 4
gpt4 key购买 nike

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

关于python - 无法捕获连接 pymysql 上的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38920764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com