2

I am trying to get information from my database and put it into a string to print it on screen. i thought i could use the code below to do it but it gives out some information about the cursor instead of the information inside the cursor.

datasource = new DataBaseHelper(this);
datasource.open();
Cursor c = datasource.getAllGoals();
startManagingCursor(c);
String g = c.toString();
goal.setText(g);
datasource.close();
Darren Murtagh
  • 591
  • 2
  • 6
  • 28
  • how information? could you more specificate it? with `String g = c.toString()` you only get `string` representation of `cursor object`.for getting informations from cursor you should use `getters` and method which allow to you passing via data. – Simon Dorociak May 26 '12 at 16:53

2 Answers2

1

The cursor can be thought of as a pointer to some underlying data. Running c.toString() on the cursor object would print the default Cursor class' implementation of its string representation (an at-sign character @ and the unsigned hexadecimal representation of the hash code of the object) which is not what you want.

To retrieve underlying database data, you will need to call c.getString(columnIndex) (source), or whatever column datatype you require for that particular column index.

Here's an example modified from source:

Say you have created a table

private static final String DATABASE_CREATE = 
            "create table comments ( "
            + "_id integer primary key autoincrement, "
            + "comment text not null);";

and your getAllGoals function returns a cursor which points to data about both _id and comment. Now you only want to show the details about the comment column. So you have to run c.getString(1). Suppose your getAllGoals function returns a cursor which only points to data about the comment column. Now you have to run c.getString(0).

I would recommend that you download the source code in the provided example and go through how data is retrieved from a cursor.

EDIT:

    public List<Comment> getAllComments() {
        List<Comment> comments = new ArrayList<Comment>();

        Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
                allColumns, null, null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {//retrieve data from multiple rows
            Comment comment = cursorToComment(cursor);
            comments.add(comment);
            cursor.moveToNext();
        }
        // Make sure to close the cursor
        cursor.close();
        return comments;
    }

    private Comment cursorToComment(Cursor cursor) {
        Comment comment = new Comment();
        comment.setId(cursor.getLong(0));
        comment.setComment(cursor.getString(1));
        return comment;
    }

source

Dhruv Gairola
  • 9,102
  • 5
  • 39
  • 43
  • what does column index refer to. would i need to change that or is that just what is needed> – Darren Murtagh May 26 '12 at 17:00
  • so if i was getting say several bits of information from the database i would run this several times for each bit of information? – Darren Murtagh May 26 '12 at 17:14
  • if you mean that getAllGoals returns several pieces of information, you will need to use a while loop. check out my answer again for an updated example. – Dhruv Gairola May 26 '12 at 17:16
  • note that you will need to know what type of data is being pointed to by the cursor returned by your getAllGoals function. this way, you can call the appropriate getter method on your cursor. – Dhruv Gairola May 26 '12 at 17:20
  • ok so if it was a get int its getInteger(0); i see. though when i ran the get string it crashed the application – Darren Murtagh May 26 '12 at 17:22
  • what does your getAllGoals function look like? what does your underlying table look like? i might be able to help you from there. – Dhruv Gairola May 26 '12 at 17:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11782/discussion-between-darren-murtagh-and-dhruv-gairola) – Darren Murtagh May 26 '12 at 17:25
0
openDataBase();
Cursor c = myDataBase.rawQuery("SELECT * FROM tb_aa_City where City_Name = '"+cityname+"'", null);
if (c.getCount() > 0) {
    c.moveToFirst();
    seqid = c.getString(4);
    System.out.println("In DB..getSequenceID..."+seqid);
    }
    c.close();
    myDataBase.close();
    SQLiteDatabase.releaseMemory();
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95