سؤال

I am using Python MySQLdb module. I am querying a table every 1 seconds. New rows are being added to this table all the time. The code is as follows;

def main(): 
    connectMySqlDb_tagem()

    while True:
        queryTable()
        time.sleep(1)

        closeDBconnection()   

The problem with the code is that the query does not return the latest rows. It always return the same rows. To solve this problem, I have to close the MySQL connection and make a new MySQL connection everytime. The workable code looks like this;

def main(): 

    while True:
        connectMySqlDb_tagem()
        queryTable()
        closeDBconnection() 
        time.sleep(1)

How can I avoid making a new connection everytime in order to get the latest rows?

هل كانت مفيدة؟

المحلول

Pass SQL_NO_CACHE in your SELECT query, or turn it off on a session level:

cursor.execute("SET SESSION query_cache_type = OFF")

See also:

Hope that helps.

نصائح أخرى

try:

connection.commit()

instead of disconnecting and reconnecting and see if it solves your problem.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top