samedi 19 décembre 2015

Adding new contacts in the Columns SQlite database

I am trying to Add contacts in my database.I successfully added contacts in it, but it keeps adding. i want it to only add the new ones not the previous ones.i tried insertonWithconflict but it deletes the other data in the same row. Now i made my phonenumber of contact unique

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

and here is adding into 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);
    try{
    db.insert(mDatabase.Tableinfo.contacts, null, sv);
}   catch(Exception e){
    }
}}

here is the code of getting contacts.

     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();
        }

Now am getting this error unique constraint. It will catch the exception and pass onto next one.

     Error inserting Contacts_name=Asdasd Contacts_phoneNo=(735) 736-357
                                                                         android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: Contacts_info.Contacts_phoneNo (code 2067)

am calling getContatcs() after every 5 mins so it keeps updating the contacts in database.

Aucun commentaire:

Enregistrer un commentaire