简体   繁体   中英

How to detect only the changes in the uploaded file?

If i have an xml file (uploaded to my server), and i do some processing on it to get the data from it,then store those data in my database .

If the user makes some changes (insert,update, delete),then upload the file again to my server . Is there any way to detect the changes (insert , update , delete)to update my database according to rather than just deleting all the data and inserting them again .especially ,there is some data depends on the the existing data(referential integrity)and if i delete all the data , the dependents will be deleted.(cascade delete)!!

Note:

  • I store the file hash , so i know if there is some change is occurred since the last uploading.

As Darin pointed out in his comment, this depends largely on the structure and content of both your XML file and your database.

In principle currently you delete a set of rows of a certain number of tables and (re-)insert another set of rows afterwards.

If you only want to update your database, you could - for each table - first identify all rows you would currently delete (ie do a SELECT ... WHERE instead of a DELETE ... WHERE ), prepare a set of rows you would insert and compare these two sets (lets call them DELETE and INSERT ):

  • Rows that are only in the DELETE set have to be deleted from the database
  • Rows that are only in the INSERT set have to be inserted
  • Rows that are in both the INSERT and DELETE set have to be updated.

But as mentioned, excatly how and in what order to perform these operations depends on your specific data structures. Eg if you have dependencies between tables (ie parent-child relationships), the delete operation on a parent entity has also to take care of the childs (possibly using a ON DELETE CASCADE clause in your foreign key definition). In this case you should also process parent tables befor their child tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM