how to catch specific pyodbc error message

前端 未结 5 1088
北恋
北恋 2020-12-18 21:39

I trid the following code,

import pyodbc
try:
    pyodbc.connect(\'DRIVER={%s};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s\' % (driver, server, database, uid, passwo         


        
5条回答
  •  感动是毒
    2020-12-18 21:52

    this will give you more clear and readable error message when connecting to mssql using myodbc:

    try:
        cnxn = pyodbc.connect(...)
    except pyodbc.Error as ex:
        sqlstate = ex.args[1]
        sqlstate = sqlstate.split(".")
        print(sqlstate[-3])
    

提交回复
热议问题