Solved

Python MS-SQL Update does not work

Posted on 2008-10-10
2
721 Views
Last Modified: 2012-05-05
I have a simple python script used to query/update an MS-SQL database. SQL SELECT works fine. SQL UPDATE appears to work (no error), but the database is not updated.

I have the same results with pyodbc and pymssql libraries.

Client:
Windows XP SP3
Python 2.5.2
pyodbc library 2.0.58

SQL Server 2000 (Running on the same Windows XP desktop).

The following script prints the number "1", indicating that 1 row was updated, but nothing is changed in the server. I've pasted the SQLTEXT in the example into Microsoft Query Analyzer and it updates the database just fine.

import pyodbc

cn = pyodbc.connect('DRIVER={SQL Server};SERVER=(local);DATABASE=NewProducts;UID=sa;PWD=xxxxxx')

cursor = cn.cursor()
sqltext = "update newproducts set avgrating=1 WHERE id=1829"
count = cursor.execute(sqltext)
print count

Any help is appreciated.

Thanks.
0
Comment
Question by:promediagrp
2 Comments
 
LVL 5

Accepted Solution

by:
PaulKeating earned 500 total points
ID: 22689325
Try this code snippet. You are connecting with autocommit=False (the pyodbc default). In Query Analyzer the default is True.

cn = pyodbc.connect('DRIVER={SQL Server};SERVER=(local);DATABASE=NewProducts;UID=sa;PWD=xxxxxx', autocommit=True)

Open in new window

0
 

Author Closing Comment

by:promediagrp
ID: 31505101
Thank you! I've spent hours on this.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now