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;
}
Table columns "id, pic, comment"
I want to take values of pic & comment to 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 not working.
Once the cursor's position is pointing to a valid row, the columns of the row can be read from the cursor. To read the data, the code in Listing 5.10 uses two methods from the cursor class: Cursor. getColumnIndexOrThrow() and one of the type get() methods from the Cursor class.
moveToFirst() method moves the cursor to the first row.
The basic purpose of a cursor is to point to a single row of the result fetched by the query. We load the row pointed by the cursor object. By using cursor we can save lot of ram and memory.
Execute the SELECT query using the cursor. execute() method. Get resultSet (all rows) from the cursor object using a cursor. fetchall() .
You should do it like this:
c1.getString(cursor.getColumnIndex("pic"));
and
c1.getString(cursor.getColumnIndex("comment"));
This is the way I do it
I prefer
getColumnIndex()
instead of number.
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();
Always use column name instead of position, because column position can change.
of course column name can change as well but let say if you add a new column and it position between column 1 and column 2. You need to change your code if you use number. But if you use name, you will be fine.
and it is more readable and what happened if you have 40 columns?(<-some say, it is bad)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With