Problem in inserting value where not exist in database:

I want to insert value where value is not present in database table,

problem in this code:

INSERT INTO table (...)

VALUES (...)

WHERE NOT EXISTS (...)

exists using Sqlite3?
EDIT:
INSERT OR IGNORE

Is not working in my case

Last updated:10/1/2015 8:58:30 AM

1 Answers

Tarun Kumar
Tarun Kumar

you can use this method to check the value is exist in database or not.
I hope it will be helpful for you:

public boolean isRecordExist(String TableName, String columnName,
                                        String value) {
      DbHelper  dbHelper = new DbHelper(this);
      dbHelper.createDataBase();
      SQLiteDatabase sqldb = dbHelper.openDataBase();
      String Query = 'Select * from ' + TableName + ' where ' + columnName + '='' + value + ''';
      Cursor cursor = sqldb.rawQuery(Query, null);
      if (cursor != null && cursor.getCount() <= 0) {
          cursor.close();
          return false;
      } else if (cursor != null) {
          cursor.close();
      }
      return true;
}

Answer