1

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?

guagay_wk
  • 26,337
  • 54
  • 186
  • 295

2 Answers2

1

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.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

try:

connection.commit()

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

Deniz
  • 1,481
  • 3
  • 16
  • 21