3

I have a Python script that queries a MySQL database every 5 seconds, gathering the latest three ID's for helpdesk tickets. I am using MySQLdb as my driver. But the issue is in my "while" loop, when I check if two arrays are equal. If they are NOT equal, I print "A new ticket has arrived." But this never prints! See my code:

import MySQLdb
import time

# Connect
db = MySQLdb.connect(host="MySQL.example.com", user="example", passwd="example", db="helpdesk_db", port=4040)
cursor = db.cursor()

IDarray = ([0,0,0])
IDarray_prev = ([0,0,0])

cursor.execute("SELECT id FROM Tickets ORDER BY id DESC limit 3;")
numrows = int(cursor.rowcount)
for x in range(0,numrows):
   row = cursor.fetchone()
   for num in row:
      IDarray_prev[x] = int(num)
cursor.close()
db.commit()

while 1:
   cursor = db.cursor()
   cursor.execute("SELECT id FROM Tickets ORDER BY id DESC limit 3;")

   numrows = int(cursor.rowcount)
   for x in range(0,numrows):
      row = cursor.fetchone()
      for num in row:
         IDarray[x] = int(num)

   print IDarray_prev, " --> ", IDarray
   if(IDarray != IDarray_prev): 
      print "A new ticket has arrived."

   time.sleep(5)
   IDarray_prev = IDarray
   cursor.close()
   db.commit()

Now when this ran, then I created a new ticket, the output looks like this:

[11474, 11473, 11472]  -->  [11474, 11473, 11472]
[11474, 11473, 11472]  -->  [11474, 11473, 11472]
[11474, 11473, 11472]  -->  [11474, 11473, 11472]
[11474, 11473, 11472]  -->  [11474, 11473, 11472]
[11475, 11474, 11473]  -->  [11475, 11474, 11473]
[11475, 11474, 11473]  -->  [11475, 11474, 11473]
[11475, 11474, 11473]  -->  [11475, 11474, 11473]
[11475, 11474, 11473]  -->  [11475, 11474, 11473]
[11475, 11474, 11473]  -->  [11475, 11474, 11473]

Where the format of my output is:

[Previous_Last_Ticket, Prev_2nd_to_last, Prev_3rd] --> [Current_Last, 2nd-to-last, 3rd]

Notice the change in numbers and, more importantly, the lack of "A new ticket has arrived"!

2 Answers 2

7

The problem is the following line:

IDarray_prev = IDarray

In Python, this makes IDarray_prev refer to the same underlying list as IDarray. Changes in one will be reflected in the other, because they both point to the same thing.

To make a copy of the list that you can use to compare later, try:

IDarray_prev = IDarray[:]

The [:] is Python slice notation that means "a copy of the whole list".

Sign up to request clarification or add additional context in comments.

2 Comments

Or you can use the copy function in the copy module.
Yes, there are many ways to solve this. You could use copy, or you could also use list(IDarray).
2

Python works with references, so you basically change both lists after the first while iteration (since they both have the same reference after you assign IDarray to IDarray_prev).

Try assigning a copy of IDarray using IDArray_prev = list(IDarray).

1 Comment

Fixed your code formatting. Note the backticks for inline code, and four-space indent for code blocks. Thanks for contributing!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.