Hi I’m trying to create a simple music library for my application that uses sqlite to store information about the files. I want to know how to update my database when the files making up the library change.
-
One way I thought of figuring out whether or not I need to update the db is to count the number of files that make up the library vs. the number of files in the database. This method seems like it would take a lot of time if there were thousands of files though. Is there a better way to do this?
-
How do I ‘update’ my db without actually going through each file again, getting file information such as artist, album, etc, and comparing it with my db to see if I need to add or remove the file from the database?
You can use several techniques.
Checking the timestamp of the files (both created and modified) will generally give you a good idea of what updates need to be made to the database if any at all.
You can also alert the user to possible changes and asking them if they want to rescan based on the type of changes detected. Traversing subdirectories and processing a few thousand files is probably not going to take as much time as you think.
When rescanning a file you don’t really need to compare it to the existing record in the database. Just execute an SQL UPDATE and be done with it. If you aren’t already familiar with SQL I suggest you study and experiment with some of the example statements available around the net. Keep in mind that if you execute a single SQL transaction for each file scanned it will take considerably longer.