I got this class, which creates database at the 1st run of the app. I wanted to fill one table with some example values from strings.xml. Basically I'd do this like:
private static final String CATEGORIES_FILL = "INSERT INTO categories VALUES ('', " + getString(R.string.fuel) + "); " +
"INSERT INTO categories VALUES ('', " + getString(R.string.food) + "); " +
"INSERT INTO categories VALUES ('', " + getString(R.string.treatment) + "); " +
"INSERT INTO categories VALUES ('', " + getString(R.string.salary) + "); ";
The problem is, the class is not an activity, and string must be static so I can use
db.execSQL(CATEGORIES_FILL);
Please correct my approach so I could use strings.xml to do that.
I assume I could do that in my activity class surrounding it with try block, but I don't like the idea of executing this every time I open the activity.
Fragment of database class
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
final String CATEGORIES_FILL = "INSERT INTO categories VALUES ('', " + context.getString(R.string.fuel) + "); " +
"INSERT INTO categories VALUES ('', " + context.getString(R.string.food) + "); " +
"INSERT INTO categories VALUES ('', " + context.getString(R.string.treatment) + "); " +
"INSERT INTO categories VALUES ('', " + context.getString(R.string.salary) + "); ";
}
/* Tworzenie tabel
* @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
db.execSQL(CATEGORIES_FILL); //need the way to access this final here
db.execSQL(EXPENSES_CREATE);
db.execSQL(INCOMES_CREATE);
db.execSQL(BUGS_CREATE);
}