2

I've got a small bit of python code that queries mySQL,

while True:
    print "running SQL query"
    cursor.execute('select * from sites where stage="NEW"')
    results = cursor.fetchall() #Fetch results
    for row in results:
        print "change detected" #simplified line for testing
    print "sleeping"
    sleep(10)

When no records match output is :

running SQL query
sleeping
running SQL query
sleeping

Manually create SQL record that will match (allow time for loop to repeat), output is still :

running SQL query
sleeping
running SQL query
sleeping

Stop script and restart, test output is as expected :

running SQL query
change detected
sleeping

So the results are being cached? or the query not being re-run?

How would I deal with this, bearing in mind the query is run quite frequently (I did wonder about closing the DB connection and re-opening everytime, but this seems excessive?)

1 Answer 1

1

If you look the Documentation for commit() it can give a hint on why your code doesn't work.

Try using database.commit() after sleep(10) and see if it works.

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

1 Comment

Thanks, this appears to work. I am still a little confused since my code doesn't write to the database (and I thought a commit was only to apply changes). I'm assuming for now, that even though this code is not making any changes, the commit is 'completing/closing' the current query such that when the query is repeated (in my code), I am looking at a fresh set of results and not the results of the previous query.

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.