title: Storing duplicate records in SQLite database table
body: <p>I am storing data into SQLite table, and if data already exist(s) then updating it, and i am able to do that but whenever i update my existing record it saves another copy of that record...</p>
  
  <p><strong>DatabaseDataHelper.java:-</strong></p>
  
  <pre><code>public class DatabaseDataHelper extends SQLiteOpenHelper {
  
      // Database Version
      private static final int DATABASE_VERSION = 1;
      // Database Name
      private static final String DATABASE_NAME = "Data";
      // Table Name
      private static final String TABLE_MEMBER = "Data";
  
      public DatabaseDataHelper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
  
          db.execSQL("CREATE TABLE " + TABLE_MEMBER + 
                    "(Name VARCHAR(50) UNIQUE,"
                    " Title VARCHAR(50))");
  
          Log.d("CREATE TABLE","Create Table Successfully - classs");
      }
  
      public long InsertData(String strName, String strTitle) {
  
          try {
              SQLiteDatabase db;
              db = this.getWritableDatabase();
  
              ContentValues Val = new ContentValues();
              Val.put("Name", strName); 
              Val.put("Title", strTitle);
  
              long rows = db.insert(TABLE_MEMBER, null, Val);
  
              db.close();
              return rows;
  
          } catch (Exception e) {
              return -1;
          }
      }
  
      public long UpdateData(String strName, String strTitle) {
  
          try {
              SQLiteDatabase db;
              db = this.getWritableDatabase();
  
              ContentValues Val = new ContentValues();
              Val.put("Title", strTitle);
  
              long rows = db.update(TABLE_MEMBER, Val, "Name=?",
                      new String[] { String.valueOf(strName) });
  
              db.close();
              return rows;
  
          } catch (Exception e) {
              return -1;
          }   
      }
  
      public String[] SelectData(String strName) {
  
          try {
              String arrData[] = null;    
  
               SQLiteDatabase db;
               db = this.getReadableDatabase();
  
               Cursor cursor = db.query(TABLE_MEMBER, new String[] { "*" }, 
                       "Name=?",
                       new String[] { String.valueOf(strName) }, null, null, null, null);
  
                  if(cursor != null) {
                      if (cursor.moveToFirst()) {
                          arrData = new String[cursor.getColumnCount()];
                          arrData[0] = cursor.getString(0);
                          arrData[1] = cursor.getString(1);
                      }
                  }
                  cursor.close();
                  db.close();
                  return arrData;
           } catch (Exception e) {
              return null;
           }
      }
  
      public boolean Exists(String strName) {
          SQLiteDatabase db;
          db = this.getReadableDatabase();
          Cursor cursor = db.rawQuery("select 1 from Data where Name= ?", new String[] { strImageName });
          boolean exists = (cursor.getCount() &gt; 0);
          cursor.close();
          return exists;
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
          onCreate(db);
      }
  
      public Cursor getAllData() {
           String selectQuery = "Select * from "+ TABLE_MEMBER; 
           SQLiteDatabase db = this.getReadableDatabase();
           Cursor cursor = db.rawQuery(selectQuery, null);
           return cursor;
      }
  
  }
  </code></pre>
  
  <p><strong>InsertDataActivity.java:-</strong></p>
  
  <pre><code>public boolean saveData() {
      long storeData = databaseDataHelper.InsertData(txtName.getText().toString(), editTitle.getText().toString());
  
      if (storeData &lt;=  0) {
      } else
          storeData = databaseDataHelper.UpdateData(txtName.getText().toString(),
              editTitle.getText().toString());
      return true;
  }
  </code></pre>