Skip to content

Instantly share code, notes, and snippets.

@leoshak
Created October 15, 2018 10:14
Show Gist options
  • Save leoshak/84ff72dcf3ebf19278cdebd9d8756e96 to your computer and use it in GitHub Desktop.
Save leoshak/84ff72dcf3ebf19278cdebd9d8756e96 to your computer and use it in GitHub Desktop.
//not included in model but to prepare for exam,
public List<String> viewSearchedUser(String id) {
SQLiteDatabase db = getReadableDatabase();
//retrieve the user using primary key
String[] selectionArgs = {id+"%"};
String query = "SELECT * FROM " + TABLE_NAME + " WHERE "+ _ID + " = ? ";
Cursor cursor = db.rawQuery(query, selectionArgs);
List<String> users = new ArrayList();
if (cursor.moveToFirst()) {
String value = cursor.getLong(0) + "\n" +
cursor.getString(1) + "\n" +
cursor.getString(2) + "\n" +
cursor.getString(3) + "\n" +
cursor.getString(4);
users.add(value);
}
cursor.close();
return users;
}
//not included in model but to prepare for exam,
public List<String> viewAllInfor() {
SQLiteDatabase db = getReadableDatabase();
String[] projection = {
_ID,
COLUMN_USERNAME,
COLUMN_PASSWORD,
COLUMN_DOB,
COLUMN_GENDER
};
Cursor cursor = db.query(
TABLE_NAME,
projection,
null,
null,
null,
null,
null);
//just to make things easy I change User type to String
List<String> users = new ArrayList();
while (cursor.moveToNext()) {
String value = cursor.getLong(0) + "\n" +
cursor.getString(1) + "\n" +
cursor.getString(2) + "\n" +
cursor.getString(3) + "\n" +
cursor.getString(4);
users.add(value);
}
cursor.close();
return users;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment