473,734 Members | 2,859 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jul 18 '05 #1
7 1887
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()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" <be***@benjiyor k.com> wrote in message
news:ma******** *************** *************** @python.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()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" <be***@benjiyor k.com> wrote in message
news:ma******** *************** *************** @python.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
3538
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 call last): File "dbmod.py", line 2, in ? File "odbc.pyo", line 9, in ?
3
1841
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 column number 3 (Expr1002)', 3326)
1
6899
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
2249
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 3.51.11.00. Maybe someone experiences similar problems or even has a solution ? Some feedback (to the group) would be highly appreciated. We use a Microsoft SQL-Server as Headquarter database and some Mysql-machines on Redhat as satellite-databases. We...
0
1558
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 ================================================================= 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
4
3574
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 Excel. I am trying to translate the same program over to Microsoft Access and I ran into a problem. Access locks up if the data I am querying for is not present in the database. To retrieve data from the database of the application, my Excel...
8
1896
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 to extract data from a database using ODBC. A search on the internet led me to the page at http://forums.asp.net/27667/ShowPost.aspx that contained a functional example in VB.NET. This worked on my computer. Hereunder is a snippet
1
1030
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 directory. I'm assuming my application directory is where I have my .ASPX page, right? I'm getting the error: There can be only one 'page' directive. Help appreciated. <%@ Page Language="VB" %> <%@ import Namespace="System.Data" %> <%@...
5
1967
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 System.Web.Services.Protocols;
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9452
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9237
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8187
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4551
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.