3

I am getting contact information from server when i login through account. i am storing those information in SQLite, when user login second time, I don't want to same duplicate contact inserting again into SQLite.

i tried like this but not working

boolean exist= contact_db.CheckItem(entry.getUser());               
if(!exist) {
 // insert
}else {
 // don't insert
}

code in DB class

Cursor mCursor = database.query(ContactsDB.TABLE_CONTACTS, allColumns,
                    ContactsDB.CONTACT_USERID + "= ' " + name +"'" , null, null, null, null);
    if (mCursor != null && mCursor.moveToFirst()) 
        return false;
    else return true;

How to avoid the duplicates while inserting the contacts into data base?

2
  • check the data before inserting. Commented Aug 21, 2012 at 9:42
  • you can check before inserting take all the contents of table in to arraylist and iterate it,compare to the contact to be inserting ... Commented Aug 21, 2012 at 9:42

2 Answers 2

6

Better would be to create a UNIQUE index for that column using UNIQUE Keyword. An example can be found here and also a simpler one can be found here.

After creating table you have to create a UNIQUE INDEX for row as,

CREATE UNIQUE INDEX idx_something ON Table_name 
                                          (column_name_1, column_name_2,....);
Sign up to request clarification or add additional context in comments.

1 Comment

I am using like this but no change still it's creating new row in DB. "CREATE UNIQUE INDEX "+ NEWS_TYPE +" ON " +TABLE_NEWS + TABLE_NEWS + "(" + NEWS_ID + " integer primary key autoincrement, " + NEWS_TYPE + " text ," + NEWS_NAME + " text not null UNIQUE," + NEWS_URL + " text " + "); ";
2

Check the database for the information. If the data is present in the database don't do anything if not insert the information into the database

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.