views:

83

answers:

1

I want to create two tables in DB, how do I do it using SQLiteOpenHelper descendant and overriding onCreate metod? Will the below code be ok? Seems I'm doing it wrong.

public void onCreate(SQLiteDatabase db){
      db.execSQL("CREATE TABLE "+CATEGORY_TABLE+" ("+_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
              CATEGORY_NAME+" TEXT NOT NULL);");    

      db.execSQL("CREATE TABLE "+RECIPES_TABLE+" ("+_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
              RECIPE_NAME+" TEXT NOT NULL, "+CATEGORY_ID+" INTEGER, "+ RECIPE+" TEXT NOT NULL);");
  }
A: 

In fact all the creation statements must be executed inside one execSQL statement. Another point that if your want to change your db schema you must increase the db version - then onCreate will be called again.

Vladimir Ivanov