I am trying to make an application that comes with some pre inserted rows in the database for the user to use. I have looked at lots of different questions here relating to the same topic but I am still slightly confused.
At the moment I have a DB class that contains all the table creates and functions that interact with the different tables. From my understanding of reading around the best way to have pre populated data is creating a file in the assets folder and calling that to insert the data.
As per the help of the answers below I have made some progress, I am no facing the error of not being able to access the sql file in the res folder or even in the raw folder. I have added below the snippet from the class were I am creating my tables and then attempting to call sql file using the runScript method. I have also added the directory layout of both res and raw.
Methods
public void runScript(SQLiteDatabase db, int rawResourceId, boolean okToFail)
{
Log.i("DB", "Running SQL script");
InputStream in = context.getResources().openRawResource(rawResourceId);
Scanner s = new Scanner(in);
String sql = "";
while (s.hasNext())
{
sql += " " + s.nextLine();
if (sql.endsWith(";"))
{
try
{
db.execSQL(sql);
}
catch (SQLException e)
{
if (okToFail)
Log.w(getClass().getSimpleName(), e.getMessage());
else
throw e;
}
sql = "";
}
}
s.close();
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(CREATE_TABLE_RECIPES);
db.execSQL(CREATE_TABLE_INGREDIENTS);
db.execSQL(CREATE_TABLE_CONTENTS);
db.execSQL(CREATE_TABLE_SHOPPING);
db.execSQL(CREATE_TABLE_DIRECTIONS);
db.execSQL(CREATE_TABLE_FOOD_CATEGORY);
db.execSQL(CREATE_TABLE_FOOD_LIST);
db.execSQL(CREATE_TABLE_BARCODE);
db.execSQL(CREATE_TABLE_FAVOURITES);
runScript(db, R.raw.pocket_chef_db.sql); ------Cannot resolve symbol raw
//runScript(db, R.res.database.pocket_chef_db.sql) ------Cannot resolve symbol res
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
Res - Path to sql
Res->database->pocket_chef_db.sql
Raw - Path to sql
Raw->pocket_chef_db.sql