odbc script

C

Chris

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.
 
C

Chris

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
 
S

Steve Holden

Chris said:
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

Try a conn.commit() after your loop.
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
 
P

Peter Hansen

Steve said:
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
 
M

Michele Petrazzo

Steve said:
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
 
B

Benji York

Michele said:
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.
 

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

Forum statistics

Threads
473,771
Messages
2,569,587
Members
45,098
Latest member
KetoBaseReview
Top