DeveloperLV
DeveloperLV

Reputation: 1781

How to compare last value to new value?

Goal:

I am aiming to compare the last result to new result, every time the function runs.

Code:

starttime = time.time()

def countRows():
    while True:
        #Get number of rows from SQL table
        sqlCursor = sqlConnection.cursor()

        sqlCursor.execute("Select count(*) from SalesOrder")

        rowcount = sqlCursor.fetchone()[0]

        print(rowcount)

        if rowcount != rowcount:
            print("changed")

        time.sleep(10.0 - ((time.time() - starttime) % 10.0))

countRows()

Details:

Here I am getting count from SQL table.

This is the output every 10 seconds:

1000
1000
1000
1000

If a record is added to sql table, the count obviously changes to 1001.

Problem with current code:

The if statement does not work - when the number changes. It just prints the number.

Question:

Whilst the function is running every 10 seconds. How can I trigger print("changed") if the value is not same as previous value?

Upvotes: 3

Views: 982

Answers (5)

Gabio
Gabio

Reputation: 9494

In order to detect changes in rowcount, you should track always the previous rowcount.

I would recommend you to split this code into 2 functions in order to prevent code repetition - one for retrieving the rowcount and one for changes detection.

Try this:

starttime = time.time()

def get_rowcount():
    # Get number of rows from SQL table
    sqlCursor = sqlConnection.cursor()

    sqlCursor.execute("Select count(*) from SalesOrder")

    rowcount = sqlCursor.fetchone()[0]
    return rowcount


def detect_rowcount_change():
    previous_rowcount = get_rowcount()
    while True:
        time.sleep(10.0 - ((time.time() - starttime) % 10.0))
        new_rowcount = get_rowcount()
        print(new_rowcount)
        if previous_rowcount != new_rowcount:
            print("changed")
        previous_rowcount = new_rowcount


detect_rowcount_change()

Upvotes: 1

mato0
mato0

Reputation: 28

This is never true because you are comparing the same variable to itself. That's always true.

if rowcount != rowcount:
    print("changed")

You have to create the second variable, where you would store the last known value and update it after this if statement with current value.

Upvotes: 0

Kunal Sawant
Kunal Sawant

Reputation: 493

I have added prevrowcount which at the start is set to -1 just to check first time it should go as it is .

after the first iteration it assigns rowcount to prevrowcount .

so in case rowcount changes you'r execution will go inside if because prevrowcount will not be equal ot rowcount

starttime = time.time()

def countRows():
    prevrowcount = -1
    while True:
        #Get number of rows from SQL table
        sqlCursor = sqlConnection.cursor()

        sqlCursor.execute("Select count(*) from SalesOrder")

        rowcount = sqlCursor.fetchone()[0]

        print(rowcount)

        if (rowcount != prevrowcount) and (prevrowcount !=-1):
            print("changed")
        prevrowcount = rowcount

        time.sleep(10.0 - ((time.time() - starttime) % 10.0))

Upvotes: 1

user12585655
user12585655

Reputation:

Think this will work:

starttime = time.time()

def countRows():
    prev_rowcount = None
    while True:
        #Get number of rows from SQL table
        sqlCursor = sqlConnection.cursor()

        sqlCursor.execute("Select count(*) from SalesOrder")

        rowcount = sqlCursor.fetchone()[0]

        print(rowcount)

        if rowcount != prev_rowcount and prev_rowcount != None:
            print("changed")
        prev_rowcount = rowcount
        time.sleep(10.0 - ((time.time() - starttime) % 10.0))

countRows()

Upvotes: 1

Chris Maes
Chris Maes

Reputation: 37742

you are comparing the same variable with itself:

if rowcount != rowcount:

you should use an intermediate variable to store the previous count: something like:

prev_rowcount = None
...
if prev_rowcount != None and prev_rowcount != rowcount:
    print("changed")
prev_rowcount = rowcount

Upvotes: 5

Related Questions