Home > Software engineering >  Getting a current or previous value and querying to see if that same field has changed or not
Getting a current or previous value and querying to see if that same field has changed or not

Time:06-23

I'm using python and sqlite3 and I am trying to see if a value has changed or not from the last query done comparing to a new query but I am unsure how to do this because I am getting this data from a function from within another function. Here is some code

def sim_data_entry():
    data = str(get_sim_number())
    c.execute('''INSERT INTO NumData VALUES(?)''',(data))

def create_table():
    c.execute('''CREATE TABLE IF NOT EXISTS DataTable(data VARCHAR)''')

Unsure if that is enough info for you to see what I am trying to do. But I would like to make another variable inside the get_sim_num() function and somehow use it to see if the data field in the table has changed at all from the last query done.

Here is the sim reader function

def get_sim_number():
    try:
       ser=serial.Serial(port=port, baudrate=115200, timeout=1)
       ser.write(b"AT ICCID\r\n")
       response = ser.readline().decode('utf-8')
       response = response.strip().replace(" ICCID: ", "")
       ser.close()
       print(response)
    except Exception as e:
        print(e)
        response = None
    return respone

Function calls

create_sim_table()
sim_data_entry()
c.close()
conn.close()

CodePudding user response:

Save the previous value in a global variable.

global_data = None

def sim_data_entry():
    global global_data

    data = str(sim_get_number())
    if global_data is not None and data != global_data:
        print("data has changed")
    c.execute('''INSERT INTO NumData VALUES(?)''',(data))
    global_data = data
  • Related