2011-12-20 57 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) + "); "; 
      } 
相關問題