检查数据库中是否已存在记录,如果没有插入,则增加计数字段

时间:2017-12-22 19:16:38

标签: android mysql sqlite android-sqlite android-music-player

我创建了一个sqlite数据库来存储用户播放次数最多的歌曲。基本上,我正在制作" Most播放的歌曲"我的音乐应用程序的播放列表。我使用的逻辑是,当用户选择歌曲时,它被发送到数据库。如果已存在于数据库中,则整数字段将增加其值,否则该歌曲将添加到数据库中。我根据整数字段显示结果(最大整数值的歌曲将显示在顶部)。但我的应用程序不会根据整数值进行排序。我不知道出了什么问题。

添加歌曲的代码:

 public void addSong(SongInfoModel songInfoModel){


    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_ID, songInfoModel.getSongID());
    values.put(KEY_NAME, songInfoModel.getSongName());
    values.put(KEY_ARTIST, songInfoModel.getArtistName());
    values.put(KEY_DURATION, songInfoModel.getDuration());
    values.put(KEY_LAST_PLAYED, getDateTime());
    values.put(KEY_ART, songInfoModel.getAlbumIDArtwork());

    if(!CheckIsDataAlreadyInDBorNot(songInfoModel)) {
        values.put(KEY_MOST_PLAYED, count);
        Log.i("Doesn't Exists:", String.valueOf(count));
    }
    else {
        values.put(KEY_MOST_PLAYED, count++);
        Log.i("Exists:", String.valueOf(count));
    }

    db.insert(TABLE_NAME,null,values);
    db.close();

    Log.i("Time song was clicked:", getDateTime());

检查歌曲是否已存在的代码:

 public  boolean CheckIsDataAlreadyInDBorNot(SongInfoModel songInfoModel) {
    SQLiteDatabase db = this.getWritableDatabase();
    String Query = "Select * from " + TABLE_NAME + " where " + KEY_ID + " = " + songInfoModel.getSongID();
    Cursor cursor = db.rawQuery(Query, null);
    if(cursor.getCount() <= 0){
        cursor.close();
        return false;
    }
    cursor.close();
    return true;
}

显示歌曲数组的代码:

 public ArrayList<SongInfoModel> getMostPlayed(){

    ArrayList<SongInfoModel> mpList = new ArrayList<>();
    String query = "SELECT * FROM " + TABLE_NAME + " ORDER BY " + KEY_MOST_PLAYED + " DESC";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query,null);
    if(cursor.moveToFirst()){

        do{

            long id = cursor.getLong(0);
            String SongName = cursor.getString(1);
            String artistName = cursor.getString(2);
            long dur = cursor.getLong(3);
            String Art = cursor.getString(5);


            SongInfoModel sh = new SongInfoModel(id,SongName,artistName,dur,null,Art);

            mpList.add(sh);
        }while (cursor.moveToNext());
    }cursor.close();



    return mpList;

2 个答案:

答案 0 :(得分:0)

更改为布尔值CheckIsDataAlreadyInDBorNot为int CheckIsDataAlreadyInDBorNot,

public  int CheckIsDataAlreadyInDBorNot(SongInfoModel songInfoModel) {
        int mostPlayed=-1;
        SQLiteDatabase db = this.getWritableDatabase();
        String Query = "Select KEY_MOST_PLAYED from " + TABLE_NAME + " where " + KEY_ID + " = " + songInfoModel.getSongID();
        Cursor cursor = db.rawQuery(Query, null);
        if(cursor.getCount() <= 0){
            cursor.close();
            return -1;
        }else {
            if(cursor.moveToFirst()){
                mostPlayed = cursor.getInt(....)
            }


        }
        cursor.close();
        return mostPlayed;
    }

然后使用

count = CheckIsDataAlreadyInDBorNot(songInfoModel)+1;
//add count to SongInfoModel
values.put(KEY_MOST_PLAYED, count);
db.insert(TABLE_NAME,null,values);
db.close();

答案 1 :(得分:0)

如果我理解正确,如果歌曲不存在你想要添加它,如果歌曲确实存在,那么你想增加歌曲在歌曲中播放的次数。

在SQLite中,新歌= INSERT ,更改歌曲(计数)= 更新

您似乎只尝试 INSERT ,这会为歌曲添加另一行,或者由于KEY_ID已存在(或其他约束)而不添加一行。其中,取决于KEY_ID列的定义方式。我怀疑后者会解释由于计数从未增加而导致预期不能正常工作。

此外 count++ (后增量)会增加值 AFTER 值。这样即使日志中的消息表明它已经增加,也会导致计数没有增加。 ++count (预增量)会增加值 BEFORE ,因此已使用此功能。

因此我相信你需要的东西是: -

public void addSong(SongInfoModel songInfoModel){

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    if(!CheckIsDataAlreadyInDBorNot(songInfoModel)) {
        values.put(KEY_ID, songInfoModel.getSongID());
        values.put(KEY_NAME, songInfoModel.getSongName());
        values.put(KEY_ARTIST, songInfoModel.getArtistName());
        values.put(KEY_DURATION, songInfoModel.getDuration());
        values.put(KEY_LAST_PLAYED, getDateTime());
        values.put(KEY_ART, songInfoModel.getAlbumIDArtwork());
        values.put(KEY_MOST_PLAYED, count);
        Log.i("Doesn't Exists:", String.valueOf(count));
        db.insert(TABLE_NAME,null,values);
    }
    else {
        values.put(KEY_MOST_PLAYED, ++count);
        db.update(TABLE_NAME,values,
            KEY_ID + "=?,
            new String[]{songInfoModel.getSongID()}
        );
        Log.i("Exists:", String.valueOf(count));
    }

    db.close();
    Log.i("Time song was clicked:", getDateTime());
}

注 - 代码未经过检查,因此可能包含错误。