Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catching SQLAlchemy exceptions

What is the upper level exception that I can catch SQLAlechmy exceptions with ?

>>> from sqlalchemy import exc >>> dir(exc) ['ArgumentError', 'CircularDependencyError', 'CompileError', 'ConcurrentModificationError', 'DBAPIError', 'DataError', 'DatabaseError', 'DisconnectionError', 'FlushError', 'IdentifierError', 'IntegrityError', 'InterfaceError', 'InternalError', 'InvalidRequestError', 'NoReferenceError', 'NoReferencedColumnError', 'NoReferencedTableError', 'NoSuchColumnError', 'NoSuchTableError', 'NotSupportedError', 'OperationalError', 'ProgrammingError', 'SADeprecationWarning', 'SAPendingDeprecationWarning', 'SAWarning', 'SQLAlchemyError', 'SQLError', 'TimeoutError', 'UnboundExecutionError', 'UnmappedColumnError', '__builtins__', '__doc__', '__file__', '__name__', '__package__'] >>>  
like image 790
khelll Avatar asked Feb 03 '10 16:02

khelll


People also ask

What is Sqlalchemyerror?

exception sqlalchemy.exc. ArgumentError(*arg, **kw) Raised when an invalid or conflicting function argument is supplied. This error generally corresponds to construction time state errors.

How do I select in Sqlalchemy?

The select() method of table object enables us to construct SELECT expression. The resultant variable is an equivalent of cursor in DBAPI. We can now fetch records using fetchone() method. Here, we have to note that select object can also be obtained by select() function in sqlalchemy.

How do you catch errors in Python?

The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error.


1 Answers

To catch any exception SQLAlchemy throws:

from sqlalchemy import exc db.add(user) try:   db.commit() except exc.SQLAlchemyError:   pass # do something intelligent here 

See help(sqlalchemy.exc) and help(sqlalchemy.orm.exc) for a list of possible exceptions that sqlalchemy can raise.

like image 162
bbrame Avatar answered Oct 18 '22 07:10

bbrame