I have a simple app which deals with database. All the database logic is done in a separate class, and in my MainActivity class I create an instance of the db class for querying the db.
Here my question is which is better approach?
-
Return a cursor object to the calling activity.
public Cursor getAllData(){ SQLiteDatabase database = helper.getWritableDatabase(); String[] columns = {DB_Helper.UID,DB_Helper.FIRST_NAME,DB_Helper.LAST_NAME,DB_Helper.DOB}; Cursor cursor = database.query(DB_Helper.TABLE_NAME,columns,null,null,null,null,null); return cursor; }
-
Return a string after manipulating the cursor object.
public String getAllData(){ SQLiteDatabase database = helper.getWritableDatabase(); String[] columns = {DB_Helper.UID,DB_Helper.FIRST_NAME,DB_Helper.LAST_NAME,DB_Helper.DOB}; Cursor cursor = database.query(DB_Helper.TABLE_NAME,columns,null,null,null,null,null); StringBuffer stringBuffer = new StringBuffer(); while (cursor.moveToNext()){ int cid = cursor.getInt(cursor.getColumnIndex(DB_Helper.UID)); String fname = cursor.getString(cursor.getColumnIndex(DB_Helper.FIRST_NAME)); String lname = cursor.getString(cursor.getColumnIndex(DB_Helper.LAST_NAME)); String dob = cursor.getString(cursor.getColumnIndex(DB_Helper.DOB)); stringBuffer.append(cid +" "+ fname +" "+ lname +" "+ dob +"\n"); } return stringBuffer.toString(); }
-
Any other better approach?
I am complete beginner so please explain a little bit in support of you answer.
Aucun commentaire:
Enregistrer un commentaire