【发布时间】:2014-04-08 21:19:06
【问题描述】:
我想多次向表中插入记录,我可以选择使用 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);
哪一个应该是最优的?
编辑:- 我正在运行的应用程序是单线程的。
【问题讨论】:
-
如果您想要更好的性能,请使用事务。
-
我正在使用这个和类似的代码段来插入多个表,如果任何插入失败,我打算回滚所有表,所以我将它们包含在 beginTransaction 和 endTransaction 和 setTransactionSuccessful if all插入成功。你能详细说明我应该如何改变这种方法
-
到目前为止,您的方法对我来说似乎是正确的。
标签: android sqlite query-optimization android-sqlite