// Getting All Contacts public ArrayList<ProfileDetails> Get_Contacts() { try { profileDetails_list.clear(); // Select All Query String selectQuery = "SELECT * FROM " + TABLE_CONTACTS; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (cursor.moveToFirst()) { do { ProfileDetails profileDetails = new ProfileDetails(); profileDetails.setID(Integer.parseInt(cursor.getString(0))); profileDetails.setName(cursor.getString(1)); profileDetails.setPhoneNumber(cursor.getString(2)); profileDetails.setEmail(cursor.getString(3)); // Adding contact to list profileDetails_list.add(profileDetails); } while (cursor.moveToNext()); } // return contact list cursor.close(); db.close(); return profileDetails_list; } catch (Exception e) { // TODO: handle exception Log.e("all_contact", "" + e); } return profileDetails_list; }
// Adding new contact public void Add_Contact(ProfileDetails profileDetails) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, profileDetails.getName()); // Contact Name values.put(KEY_PH_NO, profileDetails.getPhoneNumber()); // Contact Phone values.put(KEY_EMAIL, profileDetails.getEmail()); // Contact Email // Inserting Row db.insert(TABLE_CONTACTS, null, values); db.close(); // Closing database connection }
// Updating single contact public int Update_Contact(ProfileDetails profileDetails) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, profileDetails.getName()); values.put(KEY_PH_NO, profileDetails.getPhoneNumber()); values.put(KEY_EMAIL, profileDetails.getEmail()); // updating row return db.update( TABLE_CONTACTS, values, KEY_ID + " = ?", new String[] {String.valueOf(profileDetails.getID())}); }