Solved

Python MS-SQL Update does not work

Posted on 2008-10-10
2
725 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

895 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

15 Experts available now in Live!

Get 1:1 Help Now