LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-05-2011, 08:10 AM   #1
tadeas
Member
 
Registered: Jul 2008
Location: Prague
Distribution: Opensuse
Posts: 40

Rep: Reputation: 5
MySQLdb Python repeating the same query returns the same data


Hello,
I have a quite serious problem with the MySQLdb library in Python (2.6 in Debian Stable as well as 2.7 in OpenSuse 12.1). An example will be probably the easiest way to show what I mean:

Code:
connection = MySQLdb.connect(...)
sql = 'SELECT * FROM file WHERE disk > 4000 and disk < 5000'
cursor = connection.cursor()
cursor.execute(sql)
data = cursor.fetchall()
...
Now change data in the server that satisfy the WHERE condition. We can test it with some other client like phpmyadmin or the CLI client. Now try to get the data in code again:
Code:
...
cursor = connection.cursor()
cursor.execute(sql)
data = cursor.fetchall()
cursor.fetchall() returns the same data as before the changes! It probably doesn't actually query the MySQL server, but returns some cached result instead?

Is this the intended behaviour? And how can I change it other than disconnect and connect again?
BTW does anyone know how to contact the MySQLdb author? I couldn't find any contact, either on PyPi as well as SF (where MySQLdb is hosted). Also, could anyone point me to the bug database?

Last edited by tadeas; 12-05-2011 at 08:17 AM.
 
Old 12-05-2011, 08:27 AM   #2
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
http://stackoverflow.com/questions/5...qldb-in-python
Quote:
connection.autocommit(True)
 
Old 12-05-2011, 09:57 AM   #3
tadeas
Member
 
Registered: Jul 2008
Location: Prague
Distribution: Opensuse
Posts: 40

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by Proud View Post
...
Thanks a million times! It works.

Strange though, that I didn't find this myself.
I suppose I should not wonder why MySQL needs to commit SELECT statements...

Thanks again!
 
Old 12-05-2011, 10:29 AM   #4
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
While this works, in general don't use autocommit in code. Always commit in a bunch after confirming that the queries executed are correct. Auto-commit inserting and deleting makes destructive changes. So avoid auto-committing and keep the option for a rollback after proper error-checking.

Last edited by vharishankar; 12-05-2011 at 10:30 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python MySQLdb create simple menu comprookie2000 Programming 0 08-16-2008 07:58 PM
Etch 4.0 stable - python 2.5 and python-mysqldb? jantman Debian 1 06-06-2008 02:34 PM
Python MySQLdb syntax issues... elvenson Programming 4 12-18-2007 12:36 PM
problem installing mysqldb for python ssaigol Slackware 1 06-15-2005 02:45 PM
Help me (python and MySQLdb module) Dark Carnival Programming 2 04-22-2004 07:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration