Home > DeveloperSection > Forums > Problem in inserting value where not exist in database:
Derek Honeybun
Derek Honeybun

Total Post:17

Points:119
Posted on    October-01-2015 1:05 AM

 SQLite Database  Sqlite  Sqlite3 
Ratings:


 1 Reply(s)
 614  View(s)
Rate this:

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



Tarun Kumar

Total Post:213

Points:1635
Posted on    October-01-2015 8:58 AM

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;
}

Don't want to miss updates? Please click the below button!

Follow MindStick