Solved

Python MS-SQL Update does not work

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
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 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…

690 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