使用strings.xml中的字符串在SQLite数据库中创建示例值

时间:2011-12-20 15:09:54

标签: android android-xml

我有这个类,它在应用程序的第一次运行时创建数据库。我想用strings.xml中的一些示例值填充一个表。基本上我会这样做:

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 block来做到这一点,但我不喜欢每次打开活动时执行此操作的想法。

数据库类的片段

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);            
    }

3 个答案:

答案 0 :(得分:2)

有点像这样的东西。

添加到res / values / < yourchoiceoffilename> .xml&将字符串引用添加到字符串数组。

<resources>
    <string-array name="categories">
        <item>@string/fuel</item>
        <item>@string/food</item>
        <item>@string/treatment</item>
        <item>@string/salary</item>
    </string-array>
</resources>

在数据库上创建SQLiteStatement。

SQLiteDatabase db = ...;
SQLiteStatement insert = db.compileStatement("INSERT INTO categories (yourcolumnnamehere) VALUES (?)");

//process each string
for (String category : getResources().getStringArray(R.array.categories)) 
{
    insert.bindValue(1, category);
    long id = insert.executeInsert(); // In case you care about the row id.
}

编辑:并且,考虑在你的班级做一些喜欢的事情(我可能在那里留下了一两个错误,但你应该得到它的要点):

private static class DatabaseHelper extends SQLiteOpenHelper {
    ...
    // Replace 'yourcolumnnamehere' with whatever your column is actually named.
    private static final String INSERT_CATEGORY = "INSERT INTO categories (yourcolumnamehere) VALUES (?)"
    ...
    ...

    private final String[] mCategories;
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mCategories = context.getResources().getStringArray(R.array.categories);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);

        // Here you create the SQLiteStatement that will be used
        // to add categories. The values are stored in mCategories.
        SQLiteStatement statement = db.compileStatement(INSERT_CATEGORY);
        for (String category : mCategories) {
            statement.bindValue(1, category);
            statement.executeInsert();
        }
        statement.close();

        db.execSQL(EXPENSES_CREATE);
        db.execSQL(INCOMES_CREATE);
        db.execSQL(BUGS_CREATE);            
    }

答案 1 :(得分:1)

<string-array name="categories">
        <item>aaaaaa</item>
        <item>bbbbbbbb</item>
        <item>cccccccc</item>
        <item>ddddd</item>
</string-array>

不是添加String,而是将字符串数组添加到string.xml 然后在Java中使用:

检索它
String[] categoriesAndDescriptions = getResources().getStringArray(R.array.categories);
    for(String cad : categoriesAndDescriptions) {
        String categoryAndDesc = cad;
        list.add(categoryAndDesc);
    }

list是ArrayList,在你得到一个值后将它存储在Arraylist中。

现在将此ArayList填充为插入查询,您可以使用list.get(0)

获取第一个值

答案 2 :(得分:0)

试试这个

private Resources mResources;

DatabaseHelper(Context context) {
   super(context, DATABASE_NAME, null, DATABASE_VERSION);

   mResources = context.getResources();

   final String CATEGORIES_FILL = "INSERT INTO categories VALUES ('', " + mResources.getString(R.string.fuel) + "); " +
                "INSERT INTO categories VALUES ('', " + mResources.getString(R.string.food) + "); " +
                "INSERT INTO categories VALUES ('', " + mResources.getString(R.string.treatment) + "); " +
                "INSERT INTO categories VALUES ('', " + mResources.getString(R.string.salary) + "); "; 
            }