android - android 中的 compileStatement 或 db.insert

标签 android sqlite query-optimization android-sqlite

我想多次将记录插入表中,我可以选择使用 compileStatement 或使用 db.insert,如下所示。

String TABLENAME = "table"; //fields in table: id, name
SQLiteStatement statement = db.compileStatement("INSERT INTO "+TABLENAME+" VALUES(?,?);");  
statement.bindLong(1,666);  
statement.bindString(2,"john");
statement.executeInsert();

String TABLENAME = "table";
ContentValues values = new ContentValues();
values.put("id", 666);
values.put("name", "john");
db.insert(TABLENAME, null, values);

哪个应该是最佳的?

编辑:- 我正在运行的应用程序是单线程的。

最佳答案

insert() 在每次调用时编译 SQL,而 compileStatement() 方法仅编译一次 SQL。当多次使用具有不同绑定(bind)参数的相同 SQL 时,compileStatement() 方法做的工作更少,速度更快。

此外,考虑将插入包装在事务中以减少等待 I/O 的时间。

关于android - android 中的 compileStatement 或 db.insert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22221086/

相关文章:

sql - 优化使用 between 子句的 SQL

MySQL 性能问题/大量数据查询缓慢

android - Kotlin 从嵌套类引用外部类

java - 使用 'adb backup' 以编程方式备份​​应用程序

android - 在 Android 模拟器上从 SD 卡读取数据库

python - 为什么我的 sqlite3 外键不起作用?

mysql - 何时在mysql表中添加索引?

Android getLaunchIntentForPackage 返回 null

android - 缓慢的音乐加载算法

android - new Canvas(bitmap) 在 galaxy s 上崩溃但在 nexus 上工作