public boolean create(UserModel user) { boolean createSuccessful = false; if (!checkIfExists(user)) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(fieldObjectFullName, user.getFullName()); values.put(fieldObjectPhone, user.getPhone()); values.put(fieldTotalQuestions, user.getTotalQuestions()); values.put(fieldNumberRightAnswers, user.getNumsRightAnswers()); values.put(fieldNumberCompletedQuestion, user.getTotalCompletedQuestions()); values.put(fieldIsCompleted, user.getIsCompleted()); createSuccessful = db.insert(tableName, null, values) > 0; if (createSuccessful) { Log.v(TAG, user.getFullName() + " created."); } db.close(); } return createSuccessful; }
public Integer updateUserInfo(UserModel user, Integer userId) { SQLiteDatabase db = this.getWritableDatabase(); Integer updateOk; Integer returnStatus; ContentValues values = new ContentValues(); values.put(fieldTotalQuestions, user.getTotalQuestions()); values.put(fieldNumberRightAnswers, user.getNumsRightAnswers()); values.put(fieldNumberCompletedQuestion, user.getTotalCompletedQuestions()); values.put(fieldIsCompleted, user.getIsCompleted()); updateOk = db.update( tableName, values, String.format("%s = ?", "id"), new String[] {userId.toString()}); if (updateOk > 0) { returnStatus = AppConstant.UPDATE_DATA_SUCCESS; } else { returnStatus = AppConstant.UPDATE_DATA_FAILED; } return returnStatus; }
public UserModel[] selectAll(String filter) { String sql = ""; if (filter.equals("")) { sql += "SELECT * FROM " + tableName; } else { sql += "SELECT * FROM " + tableName + " WHERE " + fieldObjectFullName + " LIKE '%" + filter + "%'"; } SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(sql, null); int recCount = cursor.getCount(); UserModel[] objectItemData = new UserModel[recCount]; int x = 0; if (cursor.moveToFirst()) { do { UserModel user = new UserModel(); user.setId(cursor.getInt(cursor.getColumnIndex(fieldObjectId))); user.setFullName(cursor.getString(cursor.getColumnIndex(fieldObjectFullName))); user.setPhone(cursor.getString(cursor.getColumnIndex(fieldObjectPhone))); if (!cursor.getString(cursor.getColumnIndex(fieldTotalQuestions)).equals("")) { user.setTotalQuestions( Integer.parseInt(cursor.getString(cursor.getColumnIndex(fieldTotalQuestions)))); } if (!cursor.getString(cursor.getColumnIndex(fieldNumberCompletedQuestion)).equals("")) { user.setTotalCompletedQuestions( Integer.parseInt( cursor.getString(cursor.getColumnIndex(fieldNumberCompletedQuestion)))); } if (!cursor.getString(cursor.getColumnIndex(fieldNumberRightAnswers)).equals("")) { user.setNumsRightAnswers( Integer.parseInt(cursor.getString(cursor.getColumnIndex(fieldNumberRightAnswers)))); } if (!cursor.getString(cursor.getColumnIndex(fieldIsCompleted)).equals("")) { user.setIsCompleted( Integer.parseInt(cursor.getString(cursor.getColumnIndex(fieldIsCompleted)))); } objectItemData[x] = user; x++; } while (cursor.moveToNext()); } return objectItemData; }
public boolean checkIfExists(UserModel user) { boolean recordExists = false; SQLiteDatabase db = this.getWritableDatabase(); String sql = ""; sql += "SELECT " + fieldObjectId + " FROM " + tableName; sql += " WHERE full_name LIKE '%" + user.getFullName() + "%'"; sql += " AND phone = '" + user.getPhone() + "'"; Cursor cursor = db.rawQuery(sql, null); if (cursor != null) { if (cursor.getCount() > 0) { recordExists = true; } } cursor.close(); db.close(); return recordExists; }