How to populate db from array resource?
In /values/arrays.xml, I have an array that contains values that I would like to insert into an sqlite db.
In onCreate, I'm trying to get that array and run a for loop until i = length of the开发者_StackOverflow array and then using db.execSQL("INSERT INTO... or possibly initialValues.put(...
However, I can't seem to getResources() in onCreate, so I'm not even sure if this will work. Does anybody know how best to do this?
this seems to have what your after: http://developer.android.com/guide/topics/resources/string-resource.html#StringArray
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
then just loop through it as normal
If I get this straight, you're trying to add these initial values from the dbAdapter. I see no reason why you shouldn't move this procedure to the main class. I like to keep dbAdapter's methods strictly related to creation/read/write of the database, and then I populate the list in the main Activity. You will have direct access to the resources as well.
As a performance enhancer, you should try to make insert all records in one statement such as
db.rawQuery("INSERT INTO tableName(column1, column2) VALUES (valA1, valA2), (valB1, valB2) ....
You can make this query-String by simple String operations while iterating over the array. Good luck! :-)
I needed to do the same thing. I assume you know about making the database helper class that extends SQLiteOpenHelper
so I won't go into that. This is used to create or update the database when the app is installed. When this class is called you pass it the Context
. Make another class level variable to hold the context and then initialize it in the constructor. Then you use the context to getResources()
from which you can get the array to populate your database.
private static class DatabaseHelper extends SQLiteOpenHelper {
private final Context fContext;
// constructor
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
fContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
+ FIRST_COLUMN + " INTEGER PRIMARY KEY,"
+ SECOND_COLUMN + " TEXT NOT NULL,"
+ THIRD_COLUMN + " INTEGER,"
+ FOURTH_COLUMN + " TEXT NOT NULL"
+ ");");
// Initialize the db with values from my_array.xml
final int DEFAULT_THIRD_COLUMN = 1;
final String DEFAULT_FOURTH_COLUMN = "";
ContentValues values = new ContentValues();
Resources res = fContext.getResources();
String[] myArray = res.getStringArray(R.array.my_array);
for (String item : myArray){
values.put(SECOND_COLUMN, item);
values.put(THIRD_COLUMN, DEFAULT_THIRD_COLUMN);
values.put(FOURTH_COLUMN, DEFAULT_FOURTH_COLUMN);
db.insert(TABLE_NAME, null, values);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
See this link for more help:
精彩评论