pls help me with strange result

E

eight02645999

hi
i defined a func

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, even though my r is
value of 1 and i checked the database tables, they are updated with
what i want.
somehow the condition r>0 did not work..any advice ?
thanks
 
F

Fredrik Lundh

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>
 
E

eight02645999

hi
thanks!
i used your method to rearrange the try,except clauses, and the
problem is solved.
I also redefined my funcs to get rid of the try/except clauses and
leave it to the caller to
do the job of try/except.
 
D

Dennis Lee Bieber

Slipping into the middle of the thread...
said:
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

I'm a bit surprised no mention was made of letting .execute() do the
parameter substitution. Something like...


def x_dml(SQL, conn, args=None):
....
if args:
c.execute(SQL, args)
else:
c.execute(SQL)
....

stmt = """update table1 set col = '%s' where col = '%s'
update table2 set col = '%s' where col = '%s' """

# note: is that syntax valid? it looks like TWO statements to me,
# maybe a ";" is needed after the first?

....
r = x_dml(stmt,db, (blah, blah, blah, blah) )

--
 
E

eight02645999

hi
thanks for your advice. yes,the args parameter can be included.
Yes , the syntax is valid. I tried and it works for the 2 tables. looks
like sybase knows
how to distinguish that 2 update statements..as separate ones.
cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top