连接失败不显示如下信息 File "/root/test/4.py", line 340, in <module> is_mysql_connection_successful(get_mysql_connection(),'inno') File "/root/test/4.py", line 91, in get_mysql_connection password=mysql_config['password'] File "/usr/local/python3/lib/python3.7/site-packages/pymysql/connections.py", line 358, in __init__ self.connect() File "/usr/local/python3/lib/python3.7/site-packages/pymysql/connections.py", line 664, in connect self._request_authentication() File "/usr/local/python3/lib/python3.7/site-packages/pymysql/connections.py", line 954, in _request_authentication auth_packet = self._read_packet() File "/usr/local/python3/lib/python3.7/site-packages/pymysql/connections.py", line 772, in _read_packet packet.raise_for_error() File "/usr/local/python3/lib/python3.7/site-packages/pymysql/protocol.py", line 221, in raise_for_error err.raise_mysql_exception(self._data) File "/usr/local/python3/lib/python3.7/site-packages/pymysql/err.py", line 143, in raise_mysql_exception raise errorclass(errno, errval) pymysql.err.OperationalError: (1045, "Access denied for user 'dba'@'10.10.8.11' (using password: YES)")
时间: 2023-09-17 11:12:56 浏览: 235
UTL_FILE用法详解
5星 · 资源好评率100%
如果你不希望显示完整的错误信息,只想显示自定义的错误提示,可以修改函数如下:
```pythonimport pymysql
import sys
def is_mysql_successful(Function_name, error_message):
try:
connection = Function_name
connection.close()
except pymysql.Error as e:
print(error_message)
sys.exit(1)
```
在调用该函数时,你需要传入两个参数。第一个参数是你要连接数据库的函数名,第二个参数是你想要打印的错误信息。这样,当连接失败时,函数将打印指定的信息,并且退出程序。不会显示完整的错误信息。
阅读全文