Reputation: 219
I am trying to display value of a particular row in android to check if a particular updation. Can I made it successful or not? But as the return type is cursor
I don't know how to fetch it. So pls help me.
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from calls where id=" + id + "", null);
return res;
}
Upvotes: 1
Views: 136
Reputation: 57013
There are two factors that need to be considered when retrieving data from a cursor; which row (position within the cursor) and then column and it's type.
To position the Cursor you use one of the Cursor's move
methods. These are :-
move
, which moves the cursor relevant to it's current position.moveToFirst
moveToLast
moveToNext
moveToPosition
andmoveToPrevious
All return true if the move could be performed otherwise false.
A cursor can be empty and have no rows, so movetoFirst
would return false.
One of the more common usages is :-
while (cursor.moveToNext) {
.... cursor action(s) here.
}
A Cursor will initially be positioned to before the first row i.e. it's position is -1 and can be set using moveToPosition(-1)
.
The Cursor's getPosition
method will retrieve the current position, noting that the first row is position 0.
Column's in rows are accessed according to the offset (which column) and the Cursor's get????
(where ???? represents type) methods are used to retrieve the data from the row/column (there are other get.... methods e.g. getPosition
as above). The methods for retrieving the data are:-
getBlob
(retrieves a byte array).getDouble
getFloat
getInt
getLong
getShort
getString
All take a single parameter, an integer that is the column's offset within the cursor (which is not necessarily the offset in the table).
You can use any on any type of data, with the exception of blobs. An attempt to use any, except getBlob
, on a BLOB will result in an exception.
However, the results, can vary e.g. if you use get
, for example assuming column with offset 0 contains *rumplestilskin then :-
cursor.getString(0)
will return rumplestiltskincursor.getDouble(0)
will return 0 as a double.cursor.getInt(0)
will return 0 as a float.In short, you should use the appropriate get method, otherwise the results could be confusing.
Hard coding column column offsets, often results in issues therefore it is generally better to use column names in conjunction with the Cursor's getColumnIndex
method. This takes a String, the column name, as a parameter and returns the offset of the column.
Code that could be useful (i.e. it retrieves data) could be :-
Cursor csr = db.getData(myid);
while (csr.moveToNext) {
long id_of_the_row = csr.getLong(csr.getColumnIndex(yourDatabaseHelperClass.id));
}
Noting:-
if (moveToFirst(myid)) { .... }
could be considered more correct but assuming that just a single row exists bot moveToFirst and moveToNext, as coded, produce the same result.getData
method), it also assumes that variable id
has public
access. How flexible/restricive are SQLite column types?
Are there any methods that assist with resolving common SQLite issues?
Upvotes: 0
Reputation: 6405
You can follow the below code:
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from calls where id=" + id + "", null);
if (res != null) {
res.moveToFirst();
}
String firstColumnValue = res.getString(0);
String secondColumnValue = res.getString(1);
And close the database after retrieving values:
db.close();
If you want to get column value by its name, then use the following code:
String value = res.getColumnIndex("column_name");
Upvotes: 1
Reputation: 1433
Try this...
ArrayList<HashMap<String, String>> dataList = new ArrayList<>();
call your method to get data as arraylist of hashmap
dataList.addAll(loginDataBaseAdapter.getData(id));
or
dataList = loginDataBaseAdapter.getData(id);
in db_class
public ArrayList<HashMap<String, String>> getData(String id) {
ArrayList<HashMap<String, String>> returnArray = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from calls where id=" + id + "", null);
if (res != null && res.getCount() > 0) {
if (res.moveToLast()) {
do {
HashMap<String, String> getdatamap = new HashMap<String, String>();
getdatamap.put("KEY_ONE", res.getString(1));
getdatamap.put("KEY_TWO, res.getString(2));
returnArray.add(getdatamap);
}while (res.moveToPrevious());
}
}
res.close();
return returnArray;
}
Now you can use the values in arraylist to make your view
Upvotes: 2
Reputation: 680
Get row value as a String. Then return string value.
public String getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from calls where id=" + id + "", null);
if (res.getCount() > 0) {
res.moveToFirst();
String s = res.getString(res.getColumnIndex("id"));
return s;
}
Upvotes: 0