Actually displaying my SQL Database value on my application, not the location

103 Views Asked by At

Basically, I want to return the actual value in the database and not the position that is currently being pointed to. From reading up on this, the getString method seems to be the guy that I need, but that doesn't seem to be working. Here's the code:

public Cursor fetchNote(long rowId) throws SQLException {

    Cursor mCursor =

        mDb.query(true, DATABASE_TABLE, new String[] {KEY_TITLE}, KEY_ROWID + "=" + rowId, null,
                null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();

    }

    return mCursor;

So that's my pretty standard query, which should grab a result for me (i.e. the position of my mCursor). Now say that value is 'Fred' I want to show that on my Application. This is the code I have in my activity:

TextView currentName = (TextView) findViewById(R.id.currentName);
        String me = myDatabaseHelper.fetchNote(1).toString();
        currentName.setText(me);

Now, when I run this and click on the button that has a listener for this, it shows the following android.database.sqlite.SQLiteCursor@44f78c00 as I kind of expect, but I just can't get it to show me the darn value. I think I need to incorporate mCursor.getString(mCursor.getColumnIndex("title")); or something of the sort in my query, but it's returning the mCursor value at the moment, is there a separate method I should build to return the index? Sorry if that is shadily explained, I'm just in a bit of a muddle!

If you need more code/more info let me know, thank you in advance to whoever helps me with this one.

2

There are 2 best solutions below

1
On

The cursor is the whole data structure with your information. Assuming you have executed the query and have the cursor loaded, you need to use one of the cursors functions to output the current row.

Running a toString() on the entire cursor is the output you are seeing.

On the examples I see online, I don't see them saying return mCursor... they just say return;

Also try the following:

mCursor.getString(mCursor.getColumnIndex("title"));
3
On
mCursor.getString("title");

should return title in cursor.

If you want Index, mCursor.getColumnIndex("title") should be enough.