0

So right now I'm using this solution in my app Ship an application with a database

In my main activity, I just want to test to make sure that the database is working, so all I'm doing it a simply query to get some names, all I did was add 3 lines(commented where I added them):

       DatabaseHelper myDbHelper;
       SQLiteDatabase myDb = null;

       myDbHelper = new DatabaseHelper(this);
       /*
        * Database must be initialized before it can be used. This will ensure
        * that the database exists and is the current version.
        */
        myDbHelper.initializeDataBase();
        Cursor c;
        String s = null;
        try {
           // A reference to the database can be obtained after initialization.
           myDb = myDbHelper.getWritableDatabase();
//the next 3 lines are all I added
           c = myDb.rawQuery("select name from breads", null);
           s = c.getString(1);
           c.close();
           /*
            * Place code to use database here.
            */
        } catch (Exception ex) {
           ex.printStackTrace();
        } finally {
           try {
               myDbHelper.close();
           } catch (Exception ex) {
               ex.printStackTrace();
           } finally {
               myDb.close();
           }
       }

However, s just remains empty. If I do the exact same query select name from breads in the console, I will get data. Does anyone have any ideas?

1
  • define empty please. Do you mean an empty string or null? Commented Mar 27, 2011 at 23:34

1 Answer 1

4
myDb = myDbHelper.getWritableDatabase();
//the next 3 lines are all I added
c = myDb.rawQuery("select name from breads", null);
c.moveToFirst(); // ADD THIS
s = c.getString(1);
c.close();
Sign up to request clarification or add additional context in comments.

Comments

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.