Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating example values in SQLite database using strings from strings.xml
    text
    copied!<p>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:</p> <pre><code>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) + "); "; </code></pre> <p>The problem is, the class is not an activity, and string must be static so I can use</p> <pre><code>db.execSQL(CATEGORIES_FILL); </code></pre> <p>Please correct my approach so I could use strings.xml to do that.</p> <p>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.</p> <p><strong>Fragment of database class</strong></p> <pre><code>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); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload