sql-server-如何从PyMSSQL捕获异常

我正在使用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异常,但是我尝试了各种变化,但均未成功.

如何使用输出确定捕获已引发的异常的方式?

最佳答案
经过更多的探索,我找到了一个合理的解决方案.使用原始问题中列出的代码(使用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.")

或者,可以捕获注释中match所提示的使用_mysql生成的异常. (但是,在这种情况下,由于_mssql没有Cursor属性,因此随后将数据导入Pandas数据帧时会出现问题.)

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)

希望对其他人有用.

(注意:如果第一次运行这些函数中的任何一个时,SQL Server的地址输入错误,则会引发MSSQLDriverException.如果输入了正确的服务器地址(但用户名和密码不正确),则会引发MSSQLDatabaseException.但是,如果随后输入了错误的服务器,则将继续引发MSSQLDatabaseException而不是第一种情况下的MSSQLDriverException.)

转载注明原文:sql-server-如何从PyMSSQL捕获异常 - 代码日志