odbc script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    odbc script

    Hello,
    I posted a while back about a newbie database question and got a
    lot of great help here. My script that I am creating has come a long way
    (For me!) and almost does what I need it too. I am basicly using a
    dictionary to update a access database using an odbc connector. I am able
    to connect and it seems that I am able to loop through the code to update
    all the rows I need to. The one weird bug I seem to get is what ever is the
    last loop through doesn't seem to update the database?
    Here is my code:
    *************** *************** *************** *************** *************** *******
    import dbi
    import odbc
    invdictionary = {1112:0 ,1111:0 ,1129:0 ,1139:1 ,1149:1 ,1159:0 ,1169:0
    ,1179:0 ,1189:1 ,1199:0} # ( key : value )
    invd = invdictionary.i tems() # convert to a list to loop
    through
    myconn = odbc.odbc('test py') # connect to database
    mycursor = myconn.cursor()
    for x in invd:
    mycursor.execut e('Update Categories Set StockStatus=? Where ProductID=?
    ;',(x[1], x[0])) # run my sql update
    print x[0], x[1] # Just to help me
    debug
    mycursor.close( )
    myconn.close()
    print invdictionary # Just to help me debug
    print invd # Just to help me
    debug

    Here is the output:
    *************** *************** *************** *************** *************** ******
    1189 1
    1159 0
    1129 0
    1199 0
    1169 0
    1139 1
    1111 0
    1112 0
    1179 0
    1149 1
    {1189: 1, 1159: 0, 1129: 0, 1199: 0, 1169: 0, 1139: 1, 1111: 0, 1112: 0,
    1179: 0, 1149: 1}
    [(1189, 1), (1159, 0), (1129, 0), (1199, 0), (1169, 0), (1139, 1), (1111,
    0), (1112, 0), (1179, 0), (1149, 1)]
    *************** *************** *************** *************** *************** *********
    After I run this script All the values update correctly except the 1149
    value which never changes in the database.
    I messed with this for a while and found by adding items to the dictionary
    that it never seems to update whatever is the last item to go through the
    loop.
    I thought I would float it on here and see if this isn't an obvious mistake
    that I just can't see. Any help is appreciated.


  • Benji York

    #2
    Re: odbc script

    Chris wrote:[color=blue]
    > what ever is the last loop through doesn't seem to update the
    > database?[/color]

    Try a conn.commit() after your loop.
    --
    Benji York

    Comment

    • Chris

      #3
      Re: odbc script

      Thanks Benji,
      I took your advice and added in the conn.commit() into
      the script but still had the same problem. I did some digging around the
      odbc documentation and found this bug:
      *************** *************** *************** *************** *************** *************** *************** *****
      4. Hirendra Hindocha also reports: inserting a single row into a table
      doesn't work properly unless one specifically deallocates the cursor.
      for example the following code snippet -
      conn = odbc.odbc(str)
      cur = conn.cursor()
      sql = 'insert into abc_table values(1,2,'abc ')
      cur.execute(sql )
      conn.commit()
      cur.close()
      conn.close()doe sn't work, unless you add the following lines

      cur = None
      conn = None at the end of the above code snippet. Tracing with ODBC and a
      look into odbc.cpp shows that sqlfreestmt is not being called until the
      explicit deallocation is done. [Note however, Bill Tutt seems to think that
      this problem implies a problem with the specific ODBC driver, rather than
      with the ODBC implementation of Python. I haven't a clue!]

      *************** *************** *************** *************** *************** *************** *************** ********
      I figured what the heck and added in the 2 lines specified:
      cur = None
      conn = None
      and sure enough it worked after that! I am not sure why but figure that
      when the last loop goes through it is as if it is updating 1 single row?????
      Either way it works now. Thanks for the help as I am sure I needed the
      conn.commit() as well.

      Chris

      "Benji York" <benji@benjiyor k.com> wrote in message
      news:mailman.80 37.1103465926.5 135.python-list@python.org ...[color=blue]
      > Chris wrote:[color=green]
      >> what ever is the last loop through doesn't seem to update the
      >> database?[/color]
      >
      > Try a conn.commit() after your loop.
      > --
      > Benji York[/color]


      Comment

      • Steve Holden

        #4
        Re: odbc script

        Chris wrote:
        [color=blue]
        > Thanks Benji,
        > I took your advice and added in the conn.commit() into
        > the script but still had the same problem. I did some digging around the
        > odbc documentation and found this bug:
        > *************** *************** *************** *************** *************** *************** *************** *****
        > 4. Hirendra Hindocha also reports: inserting a single row into a table
        > doesn't work properly unless one specifically deallocates the cursor.
        > for example the following code snippet -
        > conn = odbc.odbc(str)
        > cur = conn.cursor()
        > sql = 'insert into abc_table values(1,2,'abc ')
        > cur.execute(sql )
        > conn.commit()
        > cur.close()
        > conn.close()doe sn't work, unless you add the following lines
        >
        > cur = None
        > conn = None at the end of the above code snippet. Tracing with ODBC and a
        > look into odbc.cpp shows that sqlfreestmt is not being called until the
        > explicit deallocation is done. [Note however, Bill Tutt seems to think that
        > this problem implies a problem with the specific ODBC driver, rather than
        > with the ODBC implementation of Python. I haven't a clue!]
        >
        > *************** *************** *************** *************** *************** *************** *************** ********
        > I figured what the heck and added in the 2 lines specified:
        > cur = None
        > conn = None
        > and sure enough it worked after that! I am not sure why but figure that
        > when the last loop goes through it is as if it is updating 1 single row?????
        > Either way it works now. Thanks for the help as I am sure I needed the
        > conn.commit() as well.
        >
        > Chris
        >
        > "Benji York" <benji@benjiyor k.com> wrote in message
        > news:mailman.80 37.1103465926.5 135.python-list@python.org ...
        >[color=green]
        >>Chris wrote:
        >>[color=darkred]
        >>>what ever is the last loop through doesn't seem to update the
        >>>database?[/color]
        >>
        >>Try a conn.commit() after your loop.
        >>--
        >>Benji York[/color]
        >
        >
        >[/color]
        Chris:

        Please note that the odbc module is a bit long in the totth now, though
        it surely is convenient to get it with win32all. If this work is being
        done for personal use you might want to look at www.egenix.com and think
        about installing the mxODBC module, which I have used with very good
        results.

        regards
        Steve
        --
        Steve Holden http://www.holdenweb.com/
        Python Web Programming http://pydish.holdenweb.com/
        Holden Web LLC +1 703 861 4237 +1 800 494 3119

        Comment

        • Peter Hansen

          #5
          Re: odbc script

          Steve Holden wrote:[color=blue]
          > Please note that the odbc module is a bit long in the totth now, though
          > it surely is convenient to get it with win32all. If this work is being
          > done for personal use you might want to look at www.egenix.com and think
          > about installing the mxODBC module, which I have used with very good
          > results.[/color]

          Or, even if you're not using it for personal use, considering
          using mxODBC. As I understand it, it's quite available for
          non-personal use, just at a (reasonable, IMHO) price.

          (Of course, Steve knew that, but just didn't think to mention
          it. I think. :-)

          -Peter

          Comment

          • Benji York

            #6
            Re: odbc script

            Steve Holden wrote:[color=blue]
            > you might want to look at www.egenix.com and think about installing
            > the mxODBC module, which I have used with very good results.[/color]

            I'd also recommend checking out the imaginatively named adodbapi
            (http://adodbapi.sourceforge.net/) which allows you to use any ODBC
            driver through ADO. It presents a standard DB-API 2.0 interface. I've
            had good luck with it.
            --
            Benji York

            Comment

            • Michele Petrazzo

              #7
              Re: odbc script

              Steve Holden wrote:[color=blue]
              > you might want to look at www.egenix.com and think
              > about installing the mxODBC module, which I have used with very good
              > results.
              >
              > regards
              > Steve[/color]

              If you want, I have created realpyodbc, that is a class for create an
              interface between python and odbc with ctypes's help . For now it is not
              db-api 2 compatible, but I use it in production and have no problems.

              You can try it here:


              Of course, if you want to help me to make it db-api 2 compatible, you
              are welcome!

              Michele

              Comment

              • Benji York

                #8
                Re: odbc script

                Michele Petrazzo wrote:[color=blue]
                > Of course, if you want to help me to make it db-api 2 compatible, you
                > are welcome![/color]

                Unfortunately I don't have much time to volunteer, but when you embark
                on the road to DB API 2.0 compliance I have a fairly extensive test
                suite you can have.
                --
                Benji York

                Comment

                Working...