?
Solved

Python MS-SQL Update does not work

Posted on 2008-10-10
2
Medium Priority
?
760 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
PaulKeating earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
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 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…
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…

741 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