I am creating a database for an Android app I am working on. I am trying to learn how to code following the correct standards and I read that in the onCreate method of the DbHelper class is where you database gets created. I also read that it is in the onCreate method that you should populate your database with data. Is this correct? And if so how do I pass an object to the onCreate method so I can loop through it and populate the database?
public class DbHelper extends SQLiteOpenHelper
{
private static String DATABASE_NAME = "FodoSubstitutes.db";
private static String FOOD_TABLE = "Food";
//Creates the database with db name and calls onCreate().
public DbHelper(Context context)
{
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db)
{
//System.out.println("in onCreate");
//assocID food healthierFood category description count submittedBy
String sql = "CREATE TABLE IF NOT EXISTS " + FOOD_TABLE +
"(Food_ID integer primary key, " +
"Food_Name string not null, " +
"Food_Aliases string, " +
"Hints string, " +
"Category string, " +
"Subcategory string, " +
"Description string, " +
"Submission_ID int, " +
"Comment_ID int, " +
"Count int); ";
db.execSQL(sql);
}
}
My thought was to do something like this.
DbHelper.onCreate(Food myFoodObj);
but that will not work. Any thoughts? It is has got to be something simple and obvious that I am overlooking.
db.execSQL,db.queryand so forth. You'll probably need some helper methods as well, for checking the database exists and so on. Also, keep this in mind: stackoverflow.com/a/7164505/429047