I'm brand new to Android development (and mobile development in
general) so please bare with me. I'm trying to write a very simple app
that will display a list of items from a database to get myself
acquainted with the environment. Everything works fine until I try to
access the database. In my main activity class I have a method,
fillData() which creates a new cursor by calling the method
fetchAllEntries() from my database helper class. I have narrowed the
problem down to this method.

If I return null from this method, it works fine. Obviously I don't
want to return null, I want to return a Cursor. I use the following
code in the method:

String[] columns = {KEY_AUTHOR, KEY_ENTRY};
Cursor c = mDb.query(DATABASE_TABLE, columns, null, null, null, null,
null);
return c;

This code is virtually identical to the code in Google's notepad
tutorial. It should work perfectly. It doesn't - the apps just stops
responding. For completeness I'll include some other relevant code
from my database helper class. Please help me with this problem, it
has me tearing my hair out. I can't even do something so basic without
breaking this damn thing.

public class FriendBoxDbAdapter {

        public static final String KEY_AUTHOR = "author";
        public static final String KEY_ENTRY = "entry";
        public static final String KEY_ROWID = "_id";

        public static final String TAG = "FriendBoxDbAdapter";

        private DatabaseHelper mDbHelper;
        private SQLiteDatabase mDb;

        /* Database creation SQL statement */
        private static final String DATABASE_CREATE =
                        "create table inbox (_id integer primary key 
autoincrement,
"
                + "author text not null, entry text not null);";

    private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE = "inbox";
    private static final int DATABASE_VERSION = 2;

    private final Context mCtx;

    /**
     * Inner class DatabaseHelper helps us interface with the DB.
     * Pretty standard SQLite statements used to initialise/upgrade
DB.
     */
    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
                    + newVersion + ", which will destroy all old
data");
            db.execSQL("DROP TABLE IF EXISTS inbox");
            onCreate(db);
        }
    }

        /**
         * Finally, our constructor. Gives us the context for our DB
         * to be created and managed.
         * @param ctx The context we can work in
         */
    public FriendBoxDbAdapter (Context ctx) {
                mCtx = ctx;
        }

    /**
     * Opens our database using Android's getWritableDatabase
     * which is required to write to a DB.
     * @return this (self reference allows chained initialisation
calls.
     * @throws SQLException if the database can't be opened or
created.
     */
    public FriendBoxDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }

    /**
     * Fetches a cursor over all entries in the database table.
     * @return a cursor over database entries
     */
    public Cursor fetchAllEntries() {
        return null;
        /*String[] columns = {KEY_AUTHOR, KEY_ENTRY};
        Cursor c = mDb.query(DATABASE_TABLE, columns, null, null, null,
null, null);
        return c;*/
    }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to