2011-12-20 49 views
0

我得到这个类,它在应用程序的第一次运行时创建数据库。我想用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);    
    } 

回答

2

隐约的东西像这样得到第一个值。

添加到RES /价值/ <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. 
} 

编辑:而且,考虑做一些以下到您的类(我可能已经离开了一个bug或两个在那里,但你应该得到它的要点):

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

上使用id,但如果类不扩展Activity,我无法使用getResources?并且(?)地方的输入是什么 – 2011-12-20 16:24:10

+1

任何上下文都可以让你获得资源。 ?是将值绑定到预编译的SQLite语句时使用的占位符。由于你无法访问上下文,因此你在哪里做这件事。 – Jens 2011-12-21 07:24:21

+0

在databaseadapter类中创建数据库,表和东西 – 2011-12-21 13:42:57

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

而是添加字符串,添加字符串数组,string.xml 然后在Java中使用检索:

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

名单的ArrayList ,在你获得一个值之后将它存储在Arraylist中。

现在填充这个ArayList到插入查询,您可以使用list.get(0)

+0

getResources也是基于上下文类。我不能在非活动类 – 2011-12-20 16:25:36

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) + "); "; 
      } 
相关问题