By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,542 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,542 IT Pros & Developers. It's quick & easy.

odbc script

P: n/a
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.items() # convert to a list to loop
through
myconn = odbc.odbc('testpy') # connect to database
mycursor = myconn.cursor()
for x in invd:
mycursor.execute('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.
Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Chris wrote:
what ever is the last loop through doesn't seem to update the
database?


Try a conn.commit() after your loop.
--
Benji York
Jul 18 '05 #2

P: n/a
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()doesn'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" <be***@benjiyork.com> wrote in message
news:ma**************************************@pyth on.org...
Chris wrote:
what ever is the last loop through doesn't seem to update the
database?


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

Jul 18 '05 #3

P: n/a
Chris wrote:
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()doesn'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" <be***@benjiyork.com> wrote in message
news:ma**************************************@pyth on.org...
Chris wrote:
what ever is the last loop through doesn't seem to update the
database?


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


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
Jul 18 '05 #4

P: n/a
Steve Holden wrote:
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.


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
Jul 18 '05 #5

P: n/a
Steve Holden wrote:
you might want to look at www.egenix.com and think about installing
the mxODBC module, which I have used with very good results.


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
Jul 18 '05 #6

P: n/a
Steve Holden wrote:
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


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:
www.unipex.it/vario/RealPyOdbc.py

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

Michele
Jul 18 '05 #7

P: n/a
Michele Petrazzo wrote:
Of course, if you want to help me to make it db-api 2 compatible, you
are welcome!


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
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.