title: Repeatedly select latest Data from Database using python
body: <p>I am trying to store the most recent row of data from a database in variables using python, however the script is storing the row of data at execution and not the latest set of data.</p>
  
  <p>Below shows the code to pull the last row from the database.
  The database gets a new row of data every minute.</p>
  
  <pre><code>#!/usr/bin/env python
  
  #import required modules
  from time import sleep
  import MySQLdb
  
  
  #set MySQL Variables
  host = "localhost"
  user = "user"
  password = "pass"
  schema = "schema"
  
  #connect to MySQL
  db = MySQLdb.connect(host, user, password, schema)
  curs = db.cursor()
  
  #Define Sql Queries to Read the last Value in the Database
  sqlT="SELECT temperature FROM system_info ORDER BY pi_id DESC LIMIT  1"
  sqlH="SELECT humidity FROM system_info ORDER BY pi_id DESC LIMIT 1"
  sqlIP="SELECT ip_address FROM system_info ORDER BY pi_id DESC LIMIT 1"
  sqlDI1="SELECT DI1 FROM system_info ORDER BY pi_id DESC LIMIT 1"
  sqlDI2="SELECT DI2 FROM system_info ORDER BY pi_id DESC LIMIT 1"
  sqlDI3="SELECT DI3 FROM system_info ORDER BY pi_id DESC LIMIT 1"
  sqlDI4="SELECT DI4 FROM system_info ORDER BY pi_id DESC LIMIT 1"
  sqlDI5="SELECT DI5 FROM system_info ORDER BY pi_id DESC LIMIT 1"
  sqlDI6="SELECT DI6 FROM system_info ORDER BY pi_id DESC LIMIT 1"
  sqlDI7="SELECT DI7 FROM system_info ORDER BY pi_id DESC LIMIT 1"
  sqlDI8="SELECT DI8 FROM system_info ORDER BY pi_id DESC LIMIT 1"
  
  #Begin Script Infinitely
  while True:
          #Gather Values from Database
          curs.execute(sqlT)
          lastT = curs.fetchone()
          for Temp in lastT:
                  temp_reading = Temp
          curs.execute(sqlH)
          lastH = curs.fetchone()
          for Hum in lastH:
                  hum_reading = Hum
          curs.execute(sqlDI1)
          lastDI1 = curs.fetchone()
          for DI1 in lastDI1:
                  DI1_reading = DI1
          curs.execute(sqlDI2)
          lastDI2 = curs.fetchone()
          for DI2 in lastDI2:
                  DI2_reading = DI2
          curs.execute(sqlDI3)
          lastDI3 = curs.fetchone()
          for DI3 in lastDI3:
                  DI3_reading = DI3
          curs.execute(sqlDI4)
          lastDI4 = curs.fetchone()
          for DI4 in lastDI4:
                  DI4_reading = DI4
          curs.execute(sqlDI5)
          lastDI5 = curs.fetchone()
          for DI5 in lastDI5:
                  DI5_reading = DI5
          curs.execute(sqlDI1)
          lastDI6 = curs.fetchone()
          for DI6 in lastDI6:
                  DI6_reading = DI6
          curs.execute(sqlDI1)
          lastDI7 = curs.fetchone()
          for DI7 in lastDI7:
                  DI7_reading = DI7
         curs.execute(sqlDI8)
         lastDI8 = curs.fetchone()
         for DI8 in lastDI8:
                  DI8_reading = DI8
  
          #Print Values
          print(temp_reading)
          print(hum_reading)
          print(DI1_reading)
          print(DI2_reading)
          print(DI3_reading)
          print(DI4_reading)
          print(DI5_reading)
          print(DI6_reading)
          print(DI7_reading)
          print(DI8_reading)
  
          #Rest so Values change
          sleep(61)
  </code></pre>
  
  <p>The results are displayed correctly for the first iteration of the while loop, however the result do not update to the latest set of data from the database, it has stored the row data in to the variable once and is not updating the variable.</p>
  
  <p>What I get:</p>
  
  <pre><code>22
  45
  Open
  Open
  Open
  Open
  Open
  Open
  Open
  Open
  22
  45
  Open
  Open
  Open
  Open
  Open
  Open
  Open
  Open
  </code></pre>
  
  <p>What I am looking to get:</p>
  
  <pre><code>22
  45
  Open
  Open
  Open
  Open
  Open
  Open
  Open
  Open
  25
  56
  Open
  Open
  Closed
  Open
  Open
  Open
  Open
  Open
  </code></pre>
  
  <p>Thanks in advance</p>
  
  <p><strong><em>EDIT</em></strong>
  Adding in example of Table Entries to accompany post:</p>
  
  <pre><code>pi_id | time | ip_address | temperature | humidity | DI1 | DI2 | DI3 | DI4 | DI5 | DI6 | DI7 | DI8 |
  5768  | 13:45 | 10.0.0.1 |   22   |   45   | Open | Open | Open | Open | Open | Open | Open | Open |
  5769  | 13:46 | 10.0.0.1 |   25   |   56   | Open | Open | Closed | Open | Open | Open | Open | Open |
  </code></pre>
  
  <p>I run the script at 13:45, it gathers the row of data with <strong>pi_id: 5768</strong>, then a minute later a new row of data is input in to the table, when the <strong>While Loop</strong> in the code runs again, it prints the data from <strong>pi_id: 5768</strong> and not the most recent data <strong>pi_id: 5769</strong>, which is what I am trying to achieve.</p>