0

我有一个数据库。因为我无法检查设备中歌曲的添加/删除,我在每次启动我的应用程序时更新我的​​歌曲表。因此我得到了duplicate of what ever already there in database。这就是为什么我的数据库大小在每次启动时都会增加。我的问题是how to insert if any new songs are addedremoved the song from database if any old song deleted

 Cursor cursorSong = this.managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
 projection,selection, null, null);
while (cursor.moveToNext()) {

                String sdata = cursor.getString(0);
                            ContentValues initialValues = new ContentValues();

                initialValues.put(KEY_PATH, sdata);

                mDB.insert(DataHelper.SONG_TABLE, null,initialValues);
            }
4

1 回答 1

1

This is something like syncing your application DB with the songs on the phone.

To Remove deleted songs from DB: If you are maintaining file path of the song, check whether the file exists in that location. If the file doesn't exist, it is that the file/song is deleted. So, delete this entry in DB.

To add new entry in DB: Before adding new entry, check file path of new file in existing DB. If path already exists, do not insert again.

By doing this, you will be deleting unwanted entries in DB, and DB maintains only existing files on the device.

于 2013-10-26T07:37:21.200 回答