[Zope] ZSQL Catch Errors

Gabriel Genellina gagenellina at softlab.com.ar
Mon Dec 22 17:39:45 EST 2003


At 22/12/2003 21:27, you wrote:

>Error Type: sql.error
>Error Value: ('37000', -3502, '[Microsoft][Controlador ODBC Microsoft
>Access] Error de sintaxis en la instrucci\xf3n INSERT INTO.')
>----------
>
>I need to know how to catch that error value from my python script (the one
>which calls the zsql method), something like:
>
>----
>try:
>
>salida=context.qReceptores(tabla='miTabla',columnas=columnas,valores=valores
>)
>except:
>     WHAT CAN I DO TO GET THE VALUES OF THE ERROR??
>----

sql.error is a string, and Python requires exactly *that* unique string as 
an except specifier (matching clause is found by object identity).
So you have to import exactly that, and you have to make security 
declarations for it too.

In any product (let's say, SampleProduct), insert into its __init__.py:

from Products.ZODBCDA.sql import error
ModuleSecurityInfo('Products.SampleProduct').declarePublic('error')

and in your Python script:

from Products.SampleProduct import error
try:
   ...execute sql method potentially raising errors...
except error,v:
   ...process exception, v is the error value

BTW, there is code in ZODBCDA.db that pretends to reformat the error 
messages, but it's wrong and has no effect (because of the identity 
matching of except clauses).


Gabriel Genellina
Softlab SRL




More information about the Zope mailing list