473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.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
7 1867
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marc Ederis | last post by:
Hello, I'm trying to create an executable with py2exe, and it uses the odbc module. The script runs fine until I use py2exe on it and run the ..exe. Then I get: -- Traceback (most recent...
3
by: Joe | last post by:
Python 2.4 Windows XP SP2 MS Access 2000 mx.ODBC 2.0.7 Problem data truncation occuring (here's the actual error message): mxODBC.Warning: ('01004', 5, ' String data, right truncated on...
1
by: SkunkDave | last post by:
Is there a script anyone has that will automate the addition of an access database to the OBDC datasources in control panel. Thanks
2
by: uli2003wien | last post by:
Dear group, we are dealing with some very specific problems with ODBC, where a connection from SQL-Server to Mysql works with ODBC-driver 3.51.10.00 and does NOT work with ODBC-driver...
0
by: uli2003wien | last post by:
Dear MS-SQL-Server-group maybe my message for MySQL is also for you of interest, since the MS-SQL-Server and it's binary_checksum function is involved...
4
by: Dave | last post by:
Hey guys, I have an ODBC problem that has me stumped. I wrote a VBA script to run in Microsoft Excel that pulls data out of an application using that application's ODBC driver and puts it into...
8
by: acb | last post by:
Hello, I am a beginner in ASP.NET and C# having programmed in VB (not the .NET flavour) in the past. I am looking for assistance in converting a functional VB.NET aspx page to C#. I am trying...
1
by: Steve Bishop | last post by:
I'm trying to test my ODBC connection. I need to use ODBC because of the application restrictions. As per Microsoft directions for the known ODBC error, I created a bin folder in my application...
5
by: cj2 | last post by:
This code works: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Linq; using System.Web; using System.Web.Services; using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.