SQLITE批量插入以通过UPDATE和INSERT增加优化

时间:2013-10-10 19:05:27

标签: android mysql sql performance sqlite

我已经看到很多关于使用批量插入在Android上优化SQLITE的帖子 目前花费90秒进行900次插入/更新。我在它们周围添加了开始/结束事务,但只看到了一些小的改进。 所以我想添加我相信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.Update

int wok = db.update(table, values, Key + " = ?", new String[] { Item });

dh.Add

int work = (int) db.insert(table, null, values);

ContentSystem是ContentValues的列表

2 个答案:

答案 0 :(得分:1)

Try INSERT OR REPLACE,而不仅仅是更新或更新失败,后跟插入。

答案 1 :(得分:0)

您确定DatabaseHandler类中的实例化SQLiteDatabase对象与_dh相同吗?

您可能已经为_dh启动了一个事务,但这可能甚至不是实际执行任何工作的实例化对象。

相关问题