Sqlite How to get string elements using cursor

public Cursor getImages(long rowId) throws SQLException
    {
        Cursor mCursor =
                db.rawQuery("select * from Pictures WHERE id=" + rowId + ";", null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;


    }

Columns of tables "id, pic, comment"

I want to take pic and comment values ​​in a string array.

My code is:

int i=0;
        Cursor c1 = db.getImages(memberId);     
        c1.moveToFirst();
        while(c1.isLast()){
            pictures[i]=c1.getString(1);
            comments[i]=c1.getString(2);
            i++;
        }

this does not work.

+5
source share
5 answers

You should do it like this:

c1.getString(cursor.getColumnIndex("pic"));

and

c1.getString(cursor.getColumnIndex("comment"));

+10
source

just use moveToNextin your loop to repeat.

while(ct.moveToNext()){
     pictures[i]=c1.getString(1);
     comments[i]=c1.getString(2);
     i++;
}
+4
source

getColumnIndex()

.

if(cursor.moveToFirst()){
    do{
          String varaible1 = cursor.getString(cursor.getColumnIndex("column_name1"));
          String varaible2 = cursor.getString(cursor.getColumnIndex("column_name2"));

       }while (cursor.moveToNext());
}
cursor.close();

, .

, , , 1 2. , . , .

and this is more readable, and what happened if you have 40 columns? (<- let's say it's bad)

+3
source

And what you have to do is replace

c1.moveToFirst();
while(c1.isLast()){
     //your code
}

Across

//c1.moveToFirst();
while(c1.moveToNext()){
     //your code
}
+2
source
Cursor c = db.rawQuery("select username from user_information where username ='" + username_txt.getText() + "'", null);
c.moveToFirst();
if (c.moveToFirst()) {
    username = c.getString(c.getColumnIndex("username"));
}

Use this. Hope this helps you.

+1
source

All Articles