29

I'm connecting mysql on my Kivy application.

import mysql.connector
con = mysql.connector.Connect(host='XXX', port=XXX, user='XXX', password='XXX', database='XXX')
cur = con.cursor()
db = cur.execute("""select SELECT SQL_NO_CACHE * from abc""")
data = cur.fetchall()
print (data)

After inserting or deleting on table abc from another connection; i call the same query on python; but data is not updating.

I add the query "SET SESSION query_cache_type = OFF;" before select query, but it didn't work. Someone said "select NOW() ..." query is not cachable but it didn't work again. What should I do?

5
  • Transaction isolation, perchance? Commented Feb 23, 2014 at 20:37
  • I checked it now. It says "transaction-isolation = REPEATABLE-READ". Is it normal? Commented Feb 23, 2014 at 20:43
  • 1
    See stackoverflow.com/a/17589234 Commented Feb 23, 2014 at 20:46
  • I've tested "... LOCK IN SHARE MODE" query, but when i call this, I can not insert or update from another connection. I want to insert or update from another connection and I want to call select query again on Python with NEW results. Commented Feb 23, 2014 at 20:53
  • 1
    @user3051668 Does the code that inserts/updates the db call .commit() ? Commented Feb 23, 2014 at 20:58

5 Answers 5

37

I solved this by adding the code after fetchall()

con.commit()

Calling the same select query without doing a commit, won't update the results.

Sign up to request clarification or add additional context in comments.

1 Comment

Why was this answer so hard to find? I'm thankful for it. I thought I was going crazy that after updating a row in my table and running a select query again, I was still getting the old values.
17

The solution is to use:

  • Once:

    con.autocommit(True)
    
  • Or, after each select query:

    con.commit()
    

With this option, there will be a commit after each select query. Otherwise, subsequent selects will render the same result.

This error seems to be Bug #42197 related to Query cache and auto-commit in MySQL. The status is won't fix!

In a few months, this should be irrelevant because MySQL 8.0 is dropping Query Cache.

Comments

3

try this,

conn.autocommit(True);

this will auto commit after each of you select query.

1 Comment

In my case db.autocommit(True) did not work. I use mysql-connector-python-2.0.4. I had to use another entry: db.autocommit = True and this has helped.
3

I encounterd the same problem that has been solved and used the above method.

conn.commit()

and I found that different DBMS has different behavior,not all DBMS exist in the connection cache

3 Comments

Is that supposed to be an answer to the question?
Maybe,hope to have a better answer
A) do you mean below instead of above? B) no, so far your answer is hard to comprehend, and really not very helpful. "Not all DBMS exist in cache" ... as said: not helpful.
-1

The MySQL query cache is flushed when tables are modified, so it wouldn't have that effect. It's impossible to say without seeing the rest of your code, but it's most likely that your INSERT / DELETE query is failing to run.

1 Comment

No, it's not failing. Because I'm checking on HeidiSQL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.