My cose is SIMPLE. I have a SQLite-Database with three columns:
This is a method in my dataBase
-class:
public Cursor fetchMessage(String tableNane, int id) {
return myDataBase.rawQuery("SELECT rowid as _id, title FROM "+tableName
+" WHERE rowid = "+id, null);
}
This method will return only one row.
In my activity I wrote this:
Cursor cursor = myDbHelper.fetchMessage(tableName, id);
Now, I want to store the content field's text in a String.
How can this be done?
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.
Calling moveToFirst() does two things: It allows us to test whether the query returned an empty set (by testing the return value) It moves the cursor to the first result (when the set is not empty)
getColumnIndex(String columnName) Returns the zero-based index for the given column name, or -1 if the column doesn't exist. abstract int. getColumnIndexOrThrow(String columnName) Returns the zero-based index for the given column name, or throws IllegalArgumentException if the column doesn't exist.
A Cursor represents the result of a query and basically points to one row of the query result. This way Android can buffer the query results efficiently; as it does not have to load all data into memory.
Use this:
if (cursor.moveToFirst()) {
str = cursor.getString(cursor.getColumnIndex("content"));
}
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