我看过很多关于使用批量插入在 android 上优化 SQLITE 的帖子 目前执行 900 次插入/更新需要 90 秒。我在它们周围添加了 Begin/End Transaction,但只看到了微小的改进。 所以我想添加我相信 SQLiteStatement
这是我目前的代码
static ArrayList<ContentSystem> csList = new ArrayList<ContentSystem>();
..填写csList..
_dh.BeginTransaction(Table);
for(int i = 0; i < csList.size(); ++i)
{
addItem(ma, csList.get(i), dh, Table, Key);
}
_dh.setTransactionSuccessful();
_dh.EndTransaction(Table);
public static void addItem(final MainActivity ma, ContentSystem cs,
final DatabaseHandler dh, String Table, String Key)
{
worked = dh.UpdateItem(cs.cv, Table, Key, cs.UUID);
if (worked == 0)
{
worked = dh.AddData(Table, cs.cv);
if (worked <= 0)
{
ErrorLogger.AddError("Error")
}
}
}
我的问题是,如果我的 csList 包含约 1000 个项目,并且有些已经在我的列表中,有些则不在 所以我目前正在进行更新,如果更新返回 0,那么我会添加
我怎样才能让这样的东西在批量声明中工作?
更多信息
dh.更新
int wok = db.update(table, values, Key + " = ?", new String[] { Item });
dh.添加
int work = (int) db.insert(table, null, values);
ContentSystem 是 ContentValues 的列表
最佳答案
Try INSERT OR REPLACE
,而不仅仅是更新或失败的更新后跟插入。
关于android - SQLITE 批量插入以通过 UPDATE 和 INSERT 增加优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19304159/