Skip to content Skip to sidebar Skip to footer

Android Studio Sqlite Don't Insert In Database If Value Already Exists

Maybe this question is duplicate but I cant find an answer that similar to my code. I want to check database value before inserting. If value is exist, I want to skip and continue

Solution 1:

private boolean isRecordExistInDatabase(String tableName, String field, String value) {
    String query = "SELECT * FROM " + tableName + " WHERE " + field + " = '" + value + "'";
    Cursor c = db.rawQuery(query, null);
    if (c.moveToFirst()) {
        //Record exist
        c.close();
        return true;
    }
    //Record available
    c.close();
    return false;    
}


public long createPlayer1(...){
    // check if record exist in database by title number
    if(isRecordExistInDatabase(SQLITE_TABLE, KEY_NUMBER, number)){
        return 0;
    }
    // create record and insert to database normally
    ContentValues initialValues = new ContentValues();
    ...
    return mDb.insert(SQLITE_TABLE, null, initialValues);
}

Solution 2:

         public boolean rowIdExists(String StrId) {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("select id from " + TABLE_USER
            + " where id=?", new String[]{StrId});
    boolean exists = (cursor.getCount() > 0);

    return exists;
   }


    public void insertuserrate(String Str[], Context cxt) {

    // TODO Auto-generated method stub

    String strId = "";

    try {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        {
            values.put(KEY_RID, Str[0]);
            values.put(ALL_name, Str[1]);

            if (rowIdExists(Str[0])) {
                db.update(TABLE_USER, values, "id " + "=" + Str[0], null);
            } else {
                db.insert(TABLE_USER, null, values);
            }
            //database connection
        }
        try {
            db.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
       } catch (Exception e) {
        e.printStackTrace();
      }

    }

Solution 3:

You can check if number is exist in database like belows:

public long createPlayer1(String title,
                              String artist, String volume,
                              String type, String favorite, String number) {

        String query = "SELECT * FROM " + SQLITE_TABLE;
        Cursor cursor = mDb.rawQuery(query, null);
        int ColumeNumber = cursor.getColumnIndex("Number"); // your column title in the table

        while (cursor.moveToNext()) {
            String num = cursor.getString(ColumeNumber);
            if(num.equals(number)) return -1; // has existed, so not insert and return now
        }
        cursor.close();

        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_ARTIST, artist);
        initialValues.put(KEY_VOLUME, volume);
        initialValues.put(KEY_TYPE, type);
        initialValues.put(KEY_FAVORITE, favorite);
        initialValues.put(KEY_NUMBER, number);

        return mDb.insert(SQLITE_TABLE, null, initialValues);
    }

Post a Comment for "Android Studio Sqlite Don't Insert In Database If Value Already Exists"