我插入一行到数据库中使用下面的方法进行的Android项目。
I am Inserting single row into database using following method for android project.
myDB.execSQL("INSERT INTO " + Buss + " (BussName, RouteName)" + " VALUES ('buss1', 'buss2');");它做工精细。我看到这个链接Inserting在SQLite数据库多行。我尝试这个方法(插入多行)在我的Android项目,但它不能正常工作。
It work fine. And I see this link Inserting multiple rows in sqlite database. and I try this method(insert multiple rows) in my android project, but It does not work.
myDB.execSQL("INSERT INTO " + Buss + " (BussName, RouteName)" + " VALUES ('buss1', 'buss2'),('buss1', 'buss2'),('buss1', 'buss2');");怎么办呢?
推荐答案您需要调用单独的insert语句对于每一行。
You need to call separate insert statement for each row.
有关性能的原因,那么你可以每隔几个电话(让说〜20)到一个事务:
For performance reason you can group every few calls (let say ~20) into one transaction:
myDb.beginTransaction(); for(<your loop definition>){ myDb.execSQL(<your insert statement>) } myDb.setTransactionSuccessful(); myDb.endTransaction();主要的想法是不写在每一个插排物理数据库文件,但每几行。在其他有只要插入数据不坚持的驾驶这是在内存中。对于小型数据集,你可以开始交易,使所有插入和结束交易的一个街区。
The main idea is to not write physical database file on every inserted row, but every few rows. On other had as long as inserted data is not persisted on "drive" it's in the memory. For small data sets you can just start transaction, make all inserts and end transaction in one block.
有关数据越大,你应该让你的交易较小。
For bigger data you should make your transactions smaller.
使用prepared语句而不是标准的声明也是一个不错的主意,因为SQL间preTER需要一次分析查询 - 更多信息可以在这里找到:How我用prepared语句SQLite中Android中?
Using prepared statement instead of standard statement is also a good idea, as the SQL interpreter needs to parse query only once - more information can be found here: How do I use prepared statements in SQlite in Android?
更多推荐
安卓:插入多行到SQLite数据库无法正常工作
发布评论