1

I have some simple code which should create a database.

package com.webfoo.android.databaseExample;

import android.app.Activity;
import android.os.Bundle;

public class DatabaseExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        jokesHelper db = new jokesHelper(getBaseContext());
    }
}

package com.webfoo.android.databaseExample;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class jokesHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "test.db";
    private static final int DB_VERSION = 1;

    public jokesHelper(Context context) {
        super(context, DATABASE_NAME, null, DB_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("create table todo (_id integer primary key autoincrement, "
                 + "joke text not null, punch text not null);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

does any have any idea why this fails to create a database? My phone is connected to my machine and the SD isn't mounted, but it's on debug mode (i can see my add being ran on my phone).

I haven't modified the manifest either, so could that be causing any issues?

Thanks

6
  • How is it that you know the DB is not being created? Can you post the output of logcat? Do you have any Log statements in your helper's onCreate()? Commented Oct 11, 2011 at 12:01
  • The database should be saved in DATA/data/APP_NAME/databases/DATABASENAME right? Well it's not there. Commented Oct 11, 2011 at 12:31
  • @dotty you had empty onCreate function ... db was created without tables ... it will not called(onCreate) again until you increased DB_VERSION Commented Oct 11, 2011 at 12:33
  • I'm sorry, but the onCreate is not empty. Commented Oct 11, 2011 at 12:38
  • What he's trying to say is that onCreate() normally only gets called once per application install (upgrades don't count). So if it was called at any point earlier in your development when perhaps it was empty, then it won't get called again unless you wipe the application's data. onUpgrade() would get called if you bumped DB_VERSION, however. Commented Oct 11, 2011 at 13:13

1 Answer 1

3

You haven't called getWritableDatabase() or getReadableDatabase() yet.

e.g.

jokesHelper dbHelper = new jokesHelper(getBaseContext());
SQLiteDatabase db = dbHelper.getWritableDatabase();

The databases are not stored on the SD card or otherwise USB-mountable storage (at least not by default). They are in "internal" storage. The path in your comment above is almost right, but I'm not sure what "DATA" means. The path is: /data/data/<package name>/databases. You can check that by using adb shell ls /data/data/<package name>/databases.

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

2 Comments

No, that's still not creating it.
Actually, logcat is telling me that it has connected to a database. Are the database not shown if i mount my card?

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.