我想多次将记录插入表中,我可以选择使用 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/