2

I have a script which I'm using to read an excel file and update an SQL database. I'm reading the excel file every 30 seconds using a loop. However I only want to update the database when the excel file changes

If I use the != operator when the loop cycles it refreshes the value of 'temp' and thus does not register that the value is the same.

Does anyone have an idea how to solve this problem..?

Thanks! edit: updated to make my problem more clear!

def update(): 
    threading.Timer(1, update).start()
    book = open_workbook('bet.xls')


    def odds():
        sheet = book.sheet_by_name('xyz')
        match_sheet = sheet.cell(5,0).value  
        data = book.sheet_by_name(sheet)
        vv = data.cell(3,26).value

        temp= None 

        if vv != temp:
            print 'hello'

        temp= vv

odds()

update()

DavidJB
  • 2,272
  • 12
  • 30
  • 37
  • Yes, you need to put the previous values in a list and compare them. What did you try already? – wRAR Feb 11 '13 at 14:11
  • Look at [`pyodbc`](https://code.google.com/p/pyodbc/wiki/GettingStarted) for the database update. Near impossible that there won't be an ODBC driver available for your DBMS. You won't get many responses unless more effort is shown in your question. Try to write the comparison and database update code yourself and let us know where you have problems. – Bryan Feb 11 '13 at 14:12
  • `elsif:` is not valid Python. – John Machin Feb 12 '13 at 10:39
  • updated, hope it is more clear now :) – DavidJB Feb 12 '13 at 22:37

1 Answers1

1

Yes, Python built-in containers are compared by value (both tuples, lists and dicts).

Something like this (I used a list comprehension to add fanciness):

//init
pvv=None

<...>

//iteration
vv= [data.cell(i,j).value for (i,j) in ((2,26),(3,26),(4,26))]
if vv!=pvv: 
    //do something
    pvv=vv
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • Thanks, that sort of works, but when the loop runs (see updated code in first post) it updates the value of None.... and hence the condition is not correct – DavidJB Feb 12 '13 at 22:35
  • You need to assign `pvv` (`temp` in your code) to None (or anything different from any possible value `vv` can take) only once, _before_ any loops. This way, it gets automatically assigned to the first value of `vv` in the 1st iteration. – ivan_pozdeev Feb 13 '13 at 08:04
  • As for the regular running, I prefer an endless loop with a `time.sleep()` - scheduling another thread every iteration is rather awkward. – ivan_pozdeev Feb 13 '13 at 08:32
  • Hi, thanks, that worked, I can't get it working my existing program structure so I just changed it like you suggested. – DavidJB Feb 13 '13 at 18:03