Stefano Mtangoo 455 Senior Poster

Implementing Save algorithm

Postby Apostle on Fri Sep 04, 2009 4:48 pm
Hello All,
I have a project of allowing user to save guests in Guest Book. I use Python/wxPython/SQLite for that. I Have implement basic database operation as well as GUI. I can save to DB as well as reload to wxListCtrl. The problem I'm facing is how to implement save algorithm.

When I save, currently, I save the whole list of object and that results in duplication each time I click save button. I want to be able to monitor changes and save ONLY the changes as well as Newly added data. I don't know how to implement that.

I don't want to write hundreds of lines of code, but here is the save function and its dependant.
Thanks

#Save Items
        def OnSave(self, evt):
            #Get Items from dictionary
            items = self.guestlist.items()
            #get tuple of data and discard the key/index
            for i, j in items:
                #Remove first element -- SQLite is set to auto increment
                self.filltb(j[1:])
               
            self.commit()
               
            #Reload after that -- Reloads data from database and fills the ListCtrl
            self.OnReload()
#populate Guest table
        def filltb(self, values, tbname = "guests"):
            query = "INSERT INTO %s(fname, lname, mphone, ministry, tdate, reason) VALUES(?, ?, ?, ?, ?, ?)" %tbname
            self.cur.execute(query, values)
#Commit all transactions
        def commit(self):
            self.conn.commit()
def OnReload(self):
            #Reload the Ctrl
            print "Reloaded called!"
            data = self.loadtb().fetchall()
            self.guestlist.clear()
            for i,j in enumerate(data):
                self.guestlist[i] = j
           
            #Load dict toListCtrl
            self.LoadData(self.guestlist)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.