1

I am new to android development and I am making a simple android app which uses sqlite to persist some data.

I have a SqliteOpenHelper which handles the database side of things. I have an onCreate method which initiates the database

Class constructor

public CardDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, CARD_DATABASE_NAME, factory, CARD_DATABASE_VERSION);
    }

Where I instantiate it in another class

    cardDBHandler = new CardDBHandler(this, null, null, 1);

The onCreate method

 @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_CARDS + "(" +
            CARD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            DECK_ID + " INTEGER, FOREIGN KEY (" + DECK_ID +") 
            REFERENCES DATABASE_NAME(COLUMN_ID), " +
            CARD_FRONT + " TEXT, " +
            CARD_BACK + " TEXT " +
            ");";
        db.execSQL(query);
        Log.d(TAG, "Created card database");
    }

I have since modified this method but my app is not registering the changes, which leads to some exceptions.

Am I supposed to add this class to the manifest the same way I add an activity? Thanks in advance.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
tinOfBeans
  • 707
  • 1
  • 9
  • 22
  • 1
    Is this code from your SQLiteOpenHelper implementation? Where do you instantiate it? – Egor May 30 '16 at 15:08
  • 1
    Are you using `SQLiteOpenHelper` ? If you're not, you'll need to call this method yourself, eg from your Activity `onCreate`, Android will not do it for you. – Jonas Czech May 30 '16 at 15:08
  • @JonasCz Yes I am using SQLiteOpenHelper. I assumed the method would be automatically called whenever I instantiated the class just like how Activities work. – tinOfBeans May 30 '16 at 15:14
  • @tinOfBeans can you put the part where you instantiate the class – Raghunandan May 30 '16 at 15:16
  • No, It's not called automatically at every start; it's called _once_, at first run of your app. You need to setup an `onUpgrade`, see Cricket's answer below. – Jonas Czech May 30 '16 at 15:21
  • @JonasCz I think it's created when you make an instance of the SqliteOpenHelper, not when the app is launched. There's nothing in the app start process that says there's a database to be used other than those classes and so nothing can be created until the database is needed – OneCricketeer May 30 '16 at 15:40
  • @cricket_007, Yes, you're right. – Jonas Czech May 30 '16 at 15:42

2 Answers2

1

If you made changes after the database was already made, then you must update the database version number and setup the onUpgrade method to drop and recreate the tables.

You can also remove some parameters from the constructor since they aren't used.

public CardDBHandler(Context context, SQLiteDatabase.CursorFactory factory) {
    super(context, CARD_DATABASE_NAME, factory, CARD_DATABASE_VERSION);
}

Then

cardDBHandler = new CardDBHandler(this, null);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Will I not need the version number in the constructor if I'm gonna update the database? I have read that the onUpgrade method is called whenever I instantiate the DB with a version number greater than the current one. – tinOfBeans May 30 '16 at 15:35
  • It looks like you've defined a constant in the SqliteOpenHelper class, so no, you shouldn't let your classes accidentally recreate your database ;) – OneCricketeer May 30 '16 at 15:37
  • And yes, it must be greater, so you started it at 1, so now make it 2. The constructor that uses the number is the super constructor, not your own – OneCricketeer May 30 '16 at 15:42
-3

you remove these lines:

super.onCreate(stuff);
setContentView(R.layout.stuff);

you need it, the first line call the onCreate father, and the second one set the view.

if you want to run these script, do it in a separated method that you will call form your onCreate

super.onCreate(stuff);
setContentView(R.layout.stuff);
doScript();




private void doScript(){
}