I am using Sqlite in Android and to obtain a value in the database I personally use something similar to this:

Cursor cursor = sqliteDatabase.rawQuery("select title,category from table", null);

int columnIndexTitle = cursor.getColumnIndex("title");
iny columnIndexCategory = cursor.getColumnIndex("category");

cursor.moveToFirst();
while (cursor.moveToNext()) {
    String title = cursor.getString(columnIndexTitle);  
    String category = cursor.getString(columnIndexCategory);    
}
cursor.close();

I wish to create my very own Cursor to ensure that I'm able to do getColumnIndex() and getString() with one way. Something similar to this:

String title = cursor.getString("title");

I wish to create my very own class that stretches the cursor which i get from sqliteDatabase.rawQuery, but I am unsure how to do this. Must I extend SQLiteCursor or how must i do that? Could it be a possible and it is it advisable?

Creating your personal getString may cause a roadmap research for every call rather than just for getColumnIndex.

Here's the code for SQLiteCursor.getColumnIndex and AbstractCursor.getColumnIndex. For those who have many rows, reducing calls for this function may prevent unnecessary string processing and map searches.

I would not extend it, I'd create a assistant:

class MartinCursor {
    private Cursor cursor;

    MartinCursor(Cursor cursor) {
        this.cursor = cursor;
    }

    String getString(String column) {
        ....
    }
}

or

class MartinCursorHelper {
    static String getString(Cursor cursor, String column) {
        ....
    }
}

Personally, I'd perform the latter, unless of course you hate supplying this extra argument constantly.

EDIT: I didn't remember to say pydave's important point: Should you refer to this as inside a loop, you are in for an obvious performance impact. The most well-liked strategy is to research the index once, cache it, and employ that rather.

You need to take advantage from the DatabaseUtils.stringForQuery() static method that's already in Android SDK to simply retrieve something, this situation is perfect for String bot there's also way of Long

stringForQuery(SQLiteDatabase db, String query, String[] selectionArgs)

Utility approach to run the query around the db and return the worthiness within the first column from the first row.

Something similar to

String myString=DatabaseUtils.stringForQuery(getDB(),query,selectionArgs);