有没有人知道在sqlite中插入行的更有效的方法?我担心分裂函数。我应该将值存储在xml还是json中?如果你有什么建议,我很想知道。
我的值数组是
private static final String[] inShopCategories =
{"Babies;5;@drawable/ic_launcher",
"Fruit / Vegetables;3;@drawable/ic_launcher",
"Meat / Fisth;2;@drawable/ic_launcher"};我的代码是:
public void onCreate(SQLiteDatabase db){
//create table
db.execSQL(cShopCateg);
//insert default shopping categories with InsertHelper
InsertHelper ih = new InsertHelper(db, tShopCateg);
final int nameCol = ih.getColumnIndex(KEY_NAME);
final int priorityCol = ih.getColumnIndex(KEY_PRIORITY);
final int iconCol = ih.getColumnIndex(KEY_ICON);
for(int i=-1,l=inShopCategories.length; ++i<l;){
ih.prepareForInsert();
String[] s = inShopCategories[i].split(";");
ih.bind(nameCol, s[0]);
ih.bind(priorityCol, s[1]);
ih.bind(iconCol, s[2]);
ih.execute();
}
}发布于 2012-01-24 02:31:30
有谁知道在
中插入行的更有效的方法吗?
是。
我担心拆分函数
这事儿可以理解。您正在无缘无故地浪费CPU周期。
我应该将值存储在xml还是json中?
这会无端浪费更多的CPU周期。
如果你有什么建议,我很想知道。
选项#1:去掉所有这些代码,使用SQLiteAssetHelper之类的东西,在应用程序中预先打包数据库,其中的数据已经插入。
选项#2:让您的String[]是完整的SQL INSERT语句并执行这些语句,而不是通过大量的解析和连接来达到相同的目的。
选项#3:如果有人用枪指着您的头来使用InsertHelper,那么为您的行和列创建一个二维String数组(String[][])。
https://stackoverflow.com/questions/8976115
复制相似问题