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();
1
  • 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. Commented May 26, 2012 at 16:53

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

7 Comments

what does column index refer to. would i need to change that or is that just what is needed>
so if i was getting say several bits of information from the database i would run this several times for each bit of information?
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.
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.
ok so if it was a get int its getInteger(0); i see. though when i ran the get string it crashed the application
|
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();

2 Comments

how do i get the c.getString(int); to point to the right information
c.getString(Colomn number) // give database Column number

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.