gpt4 book ai didi

android - 使用 Android 执行高效的 SQLite 插入

转载 作者:IT王子 更新时间:2023-10-29 06:22:14 25 4
gpt4 key购买 nike

我有一个处理我大学打印机的 Activity 。打印机可以通过互联网下载,然后存储在 SQLite 数据库中。问题是,必须创建大约 500 个数据库条目来存储它们,这对我的代码来说非常耗时(在 Google Nexus S 上大约需要 30 秒)。我这样做的代码是这样的:

printerDB = new PrinterListOpenHelper(this);
SQLiteDatabase db = printerDB.getWritableDatabase();
db.execSQL("INSERT INTO destination (id, destination) VALUES(1,'BSAC240');");
db.execSQL("INSERT INTO destination (id, destination) VALUES(2,'BSAD152');");
...

接下来是大约。 500 个相似的行。我也试过用一个

db.rawQuerry("INSERT INTO destination (id, destination) VALUES(1,'BSAC240');
INSERT INTO destination (id, destination) VALUES(2,'BSAD152');.......");

但实际上只有第一个 INSERT 语句被执行。

有人知道提高效率的技巧吗?还是 Android 数据库真的那么慢?

非常感谢您的帮助!西蒙

最佳答案

如果将它们放入事务中,效果会更好吗?

BEGIN TRANSACTION
INSERT INTO destination (id, destination) VALUES(1,'BSAC240');
INSERT INTO destination (id, destination) VALUES(2,'BSAD152');
END TRANSACTION

甚至(更新评论)

db.beginTransaction()
try {
db.execSQL("INSERT INTO destination (id, destination) VALUES(1,'BSAC240');");
db.execSQL("INSERT INTO destination (id, destination) VALUES(2,'BSAD152');");
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}

关于android - 使用 Android 执行高效的 SQLite 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5310962/

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