Prevent duplicate entries in sqlite database android

I created two tables:

  • Item.db
  • TempItem.db

Both tables have two entries when I added a third entry to the Item.db table and copy this entry to TempItem.db, this time duplicating the entry added to my TempItem table.

How to prevent this?

My source code

// Item.db table array
for(int ip=0; ip<results.size(); ip++)
{
// TempItem.db table array
    for(int ig=0; ig<resultmain.size(); ig++)
    {
        // Check Both database item
        if((results.get(ip).item).equals(resultmain.get(ig).item))
        {
            Toast.makeText(getApplicationContext(), "no", 100).show();
        }
        else
        {
            mb.insertEntry(1, 1, 1, 1, "Baby", "1", "100", "100");
        }                        
    }
}

But each time a duplicate value is added.

+4
source share
2 answers

you can use insertWithOnConflict

db.insertWithOnConflict(tableName, null, contentValues,SQLiteDatabase.CONFLICT_REPLACE);
+1
source

- . sqlite .

+1

All Articles