PROBLEM IN INSERTING VALUE WHERE NOT EXIST IN DATABASE:

Derek Honeybun

Total Post:17

Points:119
Posted by  Derek Honeybun
 1060  View(s)
Ratings:
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

  1. Tarun Kumar

    Post:214

    Points:1642
    Re: Problem in inserting value where not exist in database:

    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

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!