Solved

Python MS-SQL Update does not work

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

809 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