title: How to prevent to insert duplicate value in sqlite database (if duplicate then overwrite)
body: <p>I had created two table in my database, In both table I am inserting value at the same time, now what I want to do is that, I want to insert record in second table, but the condition is that, if there is two same record then I want insert only one record not duplicate value, In second table there is two field one is <strong>id</strong> and second is <strong>category</strong>, when user insert two same category that time I want to insert only one entry, below is my code which is not working properly, It insert all record accept duplicate value..</p>
  
  <pre><code>public long InsertCat(String idd, String cat) 
    { 
       try 
         {
          SQLiteDatabase db;
          long rows = 0;
          db = this.getWritableDatabase(); 
          ContentValues Val = new ContentValues();
          Val.put("IDD", idd); 
          Val.put("Category", cat);
          Cursor c = db.rawQuery("SELECT * FROM " + TABLE_CATEGER + " WHERE Category='"+cat+"'",null);
          while(c.moveToNext())
          {
              if(c.getString(0).equals(cat))
              {
                  flag=true;
              }
          }           
          if(flag==true)
          {
            rows=db.update(TABLE_CATEGER, Val, "Category='"+cat+"'"  , null);     
            System.out.print(rows);
            db.close();
          }
          if(flag==false)
          {
              rows = db.insert(TABLE_CATEGER, null, Val);
              System.out.print(rows);             
              db.close();
          }
          return rows; // return rows inserted.
           } catch (Exception e) {
           return -1;
           }
          }
  </code></pre>