Android SQLite how to get a specific column: row value

I have a database and I query it with

Cursor mCursor = mapDb.query(MY_TABLE, new String[] {KEY_ONEID, KEY_TWOID}, "trim("+KEY_TWOID + ") != '' ", null, null, null, null);

which in terms of SQL literally means: SELECT OneId, TwoId FROM auth WHERE trim(TwoId) != ''

Using the original SQL query, it works in my SQLite browser to show me the rows in question, so the Cursor object should contain the same results.

Secondly, in my java method, I use a condition to check if this result has anything

    if(mCursor.getColumnIndex(KEY_ONEID) > -1)  //if > -1 then the mCursor returned a row
    {

       if(mCursor.getString(mCursor.getColumnIndex(KEY_ONEID)).contains(id)) //breaks here
                return mCursor.getString(mCursor.getColumnIndex(KEY_TWOID));
            else
                return "";
    }

But for some reason, although hashmap mCursor must have all the return values, this next conditional statement

mCursor.getString(mCursor.getColumnIndex(KEY_ONEID)).contains(id)

still returns: An exception occurred: android.database.CursorIndexOutOfBoundsException

this is the line that throws the exception:, mCursor.getString(mCursor.getColumnIndex(KEY_ONEID))and my next element in the return statement

, ! , , , !

, !

+1
4

, . : mCursor.moveToFirst(). , , , ; .

, mCursor.moveToFirst(), mCursor.moveToNext(), . , false, .

, .

+6

...

Cursor c=dbhelper.getQueById(i);
if(c.getCount()>0)
{
    for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
    {
        txt1.setText(c.getString(0));
        txt2.setText(c.getString(1));
        txt3.setText(c.getString(2));   

    }
}

else
{
    Log.d(" Null value in cursor ", " null ");
}
c.close();
dbhelper.close();
+2

mCursor.moveToFirst(), mCursor.getString.

+1

cursor.moveToNext() , moveToFirst. moveToFirst -.

while (cursor.moveToNext()) {
//whatever you want to do 
}
0

All Articles