Created
October 15, 2018 10:14
-
-
Save leoshak/84ff72dcf3ebf19278cdebd9d8756e96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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