I am writing a simple query that can fetch particular fields record from a table in android.
Here is my table design:
userid, name, totalmarks, (ALL STRINGS)
here is my query:
public String getTotalMarks(String UserID)
{
String mark = "0";
SQLiteDatabase db = this.getWritableDatabase();
String queryMarks = "SELECT * FROM " +TABLE_MARK_DETAILS +" WHERE " +KEY_USERID + " =" + UserID ;
Cursor cursor = db.rawQuery(queryMarks, null);
if (cursor.moveToFirst())
{
do
{
mark = cursor.getString(0);
}
while (cursor.moveToNext());
}
Log.e("Cursor", "Value" +mark);
cursor.close();
db.close();
return mark;
}
I am getting the perfect results. I wanna know is there a better way to query the database than above?
public String getTotalMarks(String UserID) {
String mark = null;
SQLiteDatabase db = this.getReadableDatabase();
String queryMarks = "SELECT " + KEY_TOTALMARKS + " FROM " + TABLE_MARK_DETAILS + " WHERE " +KEY_USERID + " =" + UserID;
Cursor cursor = db.rawQuery(queryMarks, null);
if(null != cursor){
if(cursor.moveToFirst() && 0 < cursor.getCount()){
mark = cursor.getString(0);
}
cursor.close();
}
Log.e("Cursor", "Value" + mark);
db.close();
return TextUtils.isEmpty(mark) ? "0" : mark;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.