문제

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