Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to catch specific pyodbc error message

Tags:

I trid the following code,

import pyodbc try:     pyodbc.connect('DRIVER={%s};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s' % (driver, server, database, uid, password)) except pyodbc.Error, err:     logging.warn(err) 

The error message format i get is

('HY000', "[HY000] [MySQL][ODBC 5.1 Driver]Access denied for user 'root'@'192.168.2.27' (using password: YES) (1045) (SQLDriverConnect)") 

I want to receive just the message part of the error i.e.

Access denied for user 'root'@'192.168.2.27'(using password: YES) 

I dont know if I can catch errors specifically like, driver not found, host down etc..

I also tried catching errors as:

 except pyodbc.OperationalError, err:     logging.warn(err) except pyodbc.DataError, err:     logging.warn(err) except pyodbc.IntegrityError, err:     logging.warn(err) except pyodbc.ProgrammingError, err:     logging.warn(err) except pyodbc.NotSupportedError, err:     logging.warn(err) except pyodbc.DatabaseError, err:     logging.warn(err) except pyodbc.Error, err:     logging.warn(err) 

but the last one always catches the error.

Fruthermore i saw the pyodbc.Error.message is always empty. How can i get just the message in the error.

Thanks

like image 419
ashokadhikari Avatar asked Jul 09 '12 09:07

ashokadhikari


People also ask

What is the alternative to Pyodbc?

Another alternative is pypyodbc which was written in pure Python. it can been seen as a re-implemenation of the pyodbc module – with only around 1800 lines code, which is good for maintenance.

What is DSN in Pyodbc?

More Information. It is the name that applications use to request a connection to an ODBC Data Source. In other words, it is a symbolic name that represents the ODBC connection. It stores the connection details like database name, directory, database driver, UserID, password, etc.

Is Pyodbc part of Python?

pyodbc is an open source Python module that makes accessing ODBC databases simple. It implements the DB API 2.0 specification but is packed with even more Pythonic convenience. Precompiled binary wheels are provided for most Python versions on Windows and macOS.


1 Answers

This worked for me.

    try:         cnxn = pyodbc.connect(...)     except pyodbc.Error as ex:         sqlstate = ex.args[0]         if sqlstate == '28000':             print("LDAP Connection failed: check password") 

There are different SQLSTATES and you can have if-else statements to print out the cause.

Similarly,

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

will give you the second part of the error with description. For exampleex.args[0] give you 28000 and ex.args[1] gives [28000] LDAP authentication failed for user 'user' (24) (SQLDriverConnect)

You can then use String manipulation techniques there to just print out what you want. Hope this helps.

like image 186
Suraj Nagabhushana Ponnaganti Avatar answered Sep 20 '22 15:09

Suraj Nagabhushana Ponnaganti