我得到这个类,它在应用程序的第一次运行时创建数据库。我想用strings.xml中的一些示例值填充一个表。基本上,我会做到这一点,如:使用strings.xml中的字符串在SQLite数据库中创建示例值
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) + "); ";
的问题是,这个类是不活动,并且字符串必须是静态的,所以我可以用
db.execSQL(CATEGORIES_FILL);
请纠正我的做法,所以我可以使用strings.xml来做到这一点。
我想我可以在我的活动课中用try块来做到这一点,但我不喜欢每次打开活动时都执行此操作的想法。
数据库类的片段
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);
}
上使用id,但如果类不扩展Activity,我无法使用getResources?并且(?)地方的输入是什么 – 2011-12-20 16:24:10
任何上下文都可以让你获得资源。 ?是将值绑定到预编译的SQLite语句时使用的占位符。由于你无法访问上下文,因此你在哪里做这件事。 – Jens 2011-12-21 07:24:21
在databaseadapter类中创建数据库,表和东西 – 2011-12-21 13:42:57