Umer Asif
Umer Asif

Reputation: 441

Contacts are saved multiple times in Database

Hi guys i have code to add data into Database and that code is called on oncreate. but every time fragment is created its saves data into database again and again. I dont want that. how can i prevent that here is my code to get data and save it to database.

public void getcontacts(){
    Cursor phones;
    phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones.moveToNext()){
        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        DataBaseOperations hell = new DataBaseOperations(getActivity());
        SQLiteDatabase db = hell.getWritableDatabase();
        hell.SaveContacts(name, phoneNumber, db);
    }
    phones.close();
}

here is code for saving in database.

public void SaveContacts(String Name,String phone,SQLiteDatabase db ){
    ContentValues sv = new ContentValues();
    sv.put(mDatabase.Tableinfo.Contacts_name, Name);
    sv.put(mDatabase.Tableinfo.Contacts_phone, phone);
    db.insert(mDatabase.Tableinfo.contacts, null, sv);
}

EDIT- Here is what i have done so far but how do i set a constraint on which it will conflict.I have added an auto increment primary key as unique key but how will they conflict?? but still its saving values again in database.where do i set a primery key as constraint?????

       public void SaveContacts(String Name,String phone,SQLiteDatabase db ){
    ContentValues sv = new ContentValues();
    sv.put(mDatabase.Tableinfo.Contacts_name, Name);
    sv.put(mDatabase.Tableinfo.Contacts_phone, phone);
    db.insertWithOnConflict(mDatabase.Tableinfo.contacts, null, sv, SQLiteDatabase.CONFLICT_REPLACE);      

EDIT- 2 - how would i make unique columns in this kind of query???

    private static final String Contacts_Table = "CREATE TABLE "+
        mDatabase.Tableinfo.contacts +"("
       +mDatabase.Tableinfo.Contacts_name+" TEXT,"
        +mDatabase.Tableinfo.Contacts_phone+" TEXT NOT NULL UNIQUE,"
        +mDatabase.Tableinfo.isChattris+" TEXT,"
        +mDatabase.Tableinfo.status_contact+" TEXT,"
        +mDatabase.Tableinfo.Contact_pic+" BLOB"+")";

just Added NOT NULL UNIQUE. This is the constraint.

Upvotes: 1

Views: 177

Answers (1)

orip
orip

Reputation: 75487

  1. Keep the status of the contacts saving in unrelated storage, e.g SharedPreferences. You don't want to rely on any lifecycle triggers, not even Application.onCreate, since that will happen again and again when the app is launched.
  2. Try to update existing entries instead of adding new ones. Decide what the database key should be, and then you can use insertWithOnConflict with the CONFLICT_REPLACE flag to insert if not already in the table or update if it is.

EDIT: to define the constraints so the conflict behavior will trigger, change your CREATE TABLE statement. e.g this will cause duplicate (name,phone) pairs to trigger it. You may want to use some kind of contact ID instead though.

CREATE TABLE mytable (
  name TEXT,
  phone TEXT,
  UNIQUE(name, phone)
)

Upvotes: 1

Related Questions