gpt4 book ai didi

android - 如何使用 android sqlite 和 contentvalue 进行批量替换

转载 作者:行者123 更新时间:2023-11-30 01:12:45 26 4
gpt4 key购买 nike

我有一个 android sqlite 数据库,目前执行速度非常慢。

根据Improve INSERT-per-second performance of SQLite?的建议我已从执行更新(如果失败的插入例程)更改为仅使用替换(与 REPLACE INTO 也称为 INSERT OR REPLACE 相同)

我现在想从一次执行一个替换更改为一次执行数百个替换

static ArrayList<ContentValues> cvs= new ArrayList<ContentSystem>();



_dh.BeginTransaction(Table);
for(int i = 0; i < cvs.size(); ++i)
{
replace(ma, cvs.get(i), dh, Table, Key);
}
_dh.EndTransaction(Table);

使用批量系统

SQLiteStatement stmt = _dh.db.compileStatement("Replace into tablename(..) value (?,?)");
_dh.BeginTransaction(Table);
for(int i = 0; i < cvs.size(); ++i)
{
stmt.bindString(cvs.get(i));
}
stmt.execute();
_dh.EndTransaction(Table);

但我不明白编译语句是什么样的我也不明白我会在绑定(bind)字符串函数中放入什么 - 我将数据存储在 contentvalue 中

也来自这个http://developer.android.com/reference/android/database/sqlite/SQLiteStatement.html#execute()

Execute this SQL statement, if it is not a SELECT / INSERT / DELETE / UPDATE, for example CREATE / DROP table, view, trigger, index etc.

执行调用似乎无法与替换一起使用?因为它正在执行插入/更新这是正确的吗?

这是我的数据库的设置方式以及我如何使用替换调用 http://sqlfiddle.com/#!7/b8af8/1

最佳答案

使用准备好的语句不会对性能产生太大影响(正确使用事务更重要),但是如果你想使用它,你必须手动处理每个参数,并且您必须为每条记录调用一次execute:

db.beginTransaction();
try {
SQLiteStatement stmt = db.compileStatement(
"REPLACE INTO MyTable(x,y,z) VALUES(?,?,?)");
for (int i = 0; i < cvs.size(); ++i) {
ContentValues cv = cvs.get(i);
stmt.bindString (1, cv.getAsString ("x"));
stmt.bindString (2, cv.getAsString ("y"));
stmt.bindInteger(3, cv.getAsInteger("z"));
stmt.execute();
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}

executeexecuteInsertexecuteUpdateDelete 之间的区别仅在于返回值;如果您不需要,可以对任何语句使用execute

关于android - 如何使用 android sqlite 和 contentvalue 进行批量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19337134/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com