pls help me with strange result

Fredrik Lundh fredrik at pythonware.com
Mon Nov 7 04:24:35 EST 2005


eight02645999 at yahoo.com wrote:

> db  = Sybase.connect(DSN) ....
> ...
> def x_dml(SQL,conn):
>         ''' Connects to database specified, exec the SQL and returns
> value'''
>         try:
>                 c = conn.cursor()
>                 try:
>                         c.execute(SQL)
>                         res = c.rowcount
>                         conn.commit()
>                         return res
>                 except :
>                         return -1
>         except:
>                 return -2
> stmt = """
>     update table1 set col = '%s' where col = '%s'
>     update table2 set col = '%s' where col = '%s'
>      """ % ( blah, blah ,blah,blah)
>
> try:
>     r = x_dml(stmt,db)
>     if r > 0:
>        print r
> except:
>     print "some error"
>
>
> Whenever i execute x_dml , it raise the exeception

what exception?

to see what really happens in there, temporarily change *all*
"except:" clauses to

    except "foo":

and run your script again, and let us know what it prints.


when you've done that, change the except clauses to

   except Sybase.Error:

(you should only use catch-all except clauses if you report the error
in some other way; e.g. by inspecting sys.exc_info, or by using the
traceback module.  using catch-all to hide errors from yourself is a
rather lousy idea)


if you want to do things in a more pythonic way, remove *both*
try-except clauses from the x_dml function, and leave it to the
caller to use try-except to look for errors:

    import Sybase as DB

    conn = DB.connect(DSN) ....

    def x_dml(SQL,conn):
        c = conn.cursor()
        c.execute(SQL)
        res = c.rowcount
        conn.commit()
        return res

    ...

    try:
        r = x_dml(stmt,db)
    except DB.Error, v:
        print "some error", v
    else:
        print r, "rows updated"

</F>






More information about the Python-list mailing list