我正在使用PyMSSQL连接数据库。但是,如果我输入不正确的详细信息或存在其他问题,PyMSSQL会抛出异常。但是,我无法弄清楚如何捕获异常。我已经使用了我能想到的每一种变化,但我似乎无法以优雅的方式捕捉异常。
我的连接代码是:
import pymssql
import getpass
tempServer = input("Enter host: ")
tempUser = input("Enter user: ")
tempPwd = getpass.getpass("Enter password: ")
try:
phjTempConnection = pymssql.connect(server = tempServer,
user = tempUser,
password = tempPwd,
port = '1433')
except pymssql.MSSQLDatabaseException:
print("ERROR")
如果我在输入字段中输入废话,我会得到以下输出:
---------------------------------------------------------------------------
MSSQLDatabaseException Traceback (most recent call last)
pymssql.pyx in pymssql.connect (pymssql.c:10734)()
_mssql.pyx in _mssql.connect (_mssql.c:21821)()
_mssql.pyx in _mssql.MSSQLConnection.__init__ (_mssql.c:6581)()
_mssql.pyx in _mssql.maybe_raise_MSSQLDatabaseException (_mssql.c:17524)()
MSSQLDatabaseException: (18456, b'Unknown error')
During handling of the above exception, another exception occurred:
OperationalError Traceback (most recent call last)
<ipython-input-21-22d7fd0e3d05> in <module>()
11 password = tempPwd,
---> 12 port = '1433')
13 except pymssql.MSSQLDatabaseException:
pymssql.pyx in pymssql.connect (pymssql.c:10824)()
OperationalError: (18456, b'Unknown error')
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-21-22d7fd0e3d05> in <module>()
11 password = tempPwd,
12 port = '1433')
---> 13 except pymssql.MSSQLDatabaseException:
14 print("ERROR")
AttributeError: 'module' object has no attribute 'MSSQLDatabaseException'
我原以为输出会提供足够的信息,以便能够找出如何捕获MSSQLDatabaseException异常,但我已经尝试过各种各样的变化而没有成功。
如何使用输出来计算捕获异常的方法?
答案 0 :(得分:2)
经过一番探索,我找到了一个合理的解决方案。使用原始问题中的代码(使用pymssql.connect()),引发了两个可能的异常。如果输入了正确的服务器地址但用户名或密码不正确,则引发MSSQLDatabaseException。但是,如果服务器地址不正确,则会引发MSSQLDriverException(但请参阅结尾处的注释)。要从这些错误中正常退出,似乎有必要捕获pymssql.InterfaceError(捕获MSSQLDriverException)和pymssql.DatabaseError(捕获MSSQLDatabaseError)。
import pymssql
import getpass
tempServer = input("Enter host: ")
tempUser = input("Enter user: ")
tempPwd = getpass.getpass("Enter password: ")
try:
phjTempConnection = pymssql.connect(server = tempServer,
user = tempUser,
password = tempPwd,
port = '1433')
except pymssql.InterfaceError:
print("A MSSQLDriverException has been caught.")
except pymssql.DatabaseError:
print("A MSSQLDatabaseException has been caught.")
或者,可以捕获使用_mysql生成的异常,如注释中的匹配所示。 (但是,在这种情况下,随后将数据导入Pandas数据帧时出现问题,因为_mssql没有Cursor属性。)
import pymssql
import _mssql
import getpass
tempServer = input("Enter host: ")
tempUser = input("Enter user: ")
tempPwd = getpass.getpass("Enter password: ")
try:
phjTempConnection = _mssql.connect(server = tempServer,
user = tempUser,
password = tempPwd,
port = '1433')
except _mssql.MssqlDriverException:
print("A MSSQLDriverException has been caught.")
except _mssql.MssqlDatabaseException as e:
print("A MSSQLDatabaseException has been caught.")
print('Number = ',e.number)
print('Severity = ',e.severity)
print('State = ',e.state)
print('Message = ',e.message)
希望这对其他人有用。
(NB如果在第一次运行这些函数之一时输入的SQL SERVER的地址不正确,则会引发MSSQLDriverException。如果输入了正确的服务器地址(但用户和密码不正确),则会引发MSSQLDatabaseException但是,如果随后输入了错误的服务器,将继续引发MSSQLDatabaseException,而不是像第一种情况那样引发MSSQLDriverException。)
答案 1 :(得分:1)
文档暗示该异常是_mssql
模块的一部分,因此您可能需要在此处捕获_mssql.MSSQLDatabaseException
。