Search code examples
pythonmariadbmariadb-connect-engine

python database request not updating


I am pulling data from a database in a python script which works on start up but does not update when I change the value within the database.

The script initially connects to the database then selects all the contents where id is equal to 1 (this has a number of columns which I intend to use)

It then updates the global variable garageHeating with the contents from the garage column, lets say this is equal to 0 at start up.

The while loop them prints out this variable which on start will be 0

If I them change the column garage within the database to 10 the python script stays at 0. It does not update the global variable which I thought it should do

my script is below

import mysql.connector as mariadb

mariadb_connection = mariadb.connect(
  host="localhost",
  user="garage",
  passwd="*******",
  database="mydb"
  )
cursor = mariadb_connection.cursor()

garageHeating = 0 

def readDbHeating():
   global garageHeating
   result = []
   try:
      cursor.execute ("SELECT * FROM heating WHERE id = '1'")
      for reading in cursor.fetchall():
         result.append (reading)
         garageHeating = result[0][8]
   except () as e:
      print (e)

while 1:
   readDbHeating()
   print garageHeating

I have edited the code above shown below to show the global variable outside the function and also to print out the result within the try command. I am still getting the same issue after the first pull of data from the database the script keeps displaying the first set data but does not update to the new data if i change the database records.

import mysql.connector as mariadb

mariadb_connection = mariadb.connect(
  host="localhost",
  user="garage",
  passwd="14Odiham",
  database="mydb"
  )
cursor = mariadb_connection.cursor()

global garageHeating

def readDbHeating():
   result = []
   try:
      cursor.execute ("SELECT * FROM heating WHERE id = '1'")
      for reading in cursor.fetchall():
         result.append (reading)
         garageHeating = result[0][8]
         print garageHeating
         print result
   except () as e:
      print (e)

while 1:
    readDbHeating()
    #print garageHeating

Solution

  • You must either call MySQLdb.connections.Connection.commit before executing a query or set autocommit on the connection object.

    Commiting before query

    def readDbHeating():
       global garageHeating
       result = []
       try:
          # commit
          mariadb_connection.commit()
          cursor.execute ("SELECT * FROM heating WHERE id = '1'")
          for reading in cursor.fetchall():
             result.append (reading)
             garageHeating = result[0][8]
       except () as e:
          print (e)
    

    Autocommit when creating a connection

    mariadb_connection = mariadb.connect(
      host="localhost",
      user="garage",
      passwd="14Odiham",
      database="mydb",
      # Auto commit
      autocommit=True
      )
    

    Autocommit after connection creation

    mariadb_connection.autocommit(True)