pls help me with strange result

Discussion in 'Python' started by eight02645999@yahoo.com, Nov 7, 2005.

  1. Guest

    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
     
    , Nov 7, 2005
    #1
    1. Advertising

  2. 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>
     
    Fredrik Lundh, Nov 7, 2005
    #2
    1. Advertising

  3. Guest

    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.
     
    , Nov 7, 2005
    #3
  4. On Mon, 7 Nov 2005 10:24:35 +0100, "Fredrik Lundh"
    <> declaimed the following in comp.lang.python:

    Slipping into the middle of the thread...

    > wrote:
    >
    > > db = Sybase.connect(DSN) ....
    > > ...
    > > def x_dml(SQL,conn):

    <snip>
    > > c.execute(SQL)

    <snip>
    > > 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)

    <snip>

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

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Nov 7, 2005
    #4
  5. Guest

    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
     
    , Nov 8, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.Ram
    Replies:
    7
    Views:
    655
  2. Pavel
    Replies:
    7
    Views:
    537
    Pavel
    Sep 19, 2010
  3. Lakshmi Sreekanth

    i = 10; result = ++i - --i; How result become ZERO

    Lakshmi Sreekanth, Sep 21, 2010, in forum: C Programming
    Replies:
    52
    Views:
    1,188
    Nick Keighley
    Sep 23, 2010
  4. Mr. Buffoon
    Replies:
    4
    Views:
    312
    Eric Sosman
    Sep 23, 2010
  5. Michael Tan
    Replies:
    32
    Views:
    1,000
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page