安卓:插入多行到SQLite数据库无法正常工作 [英] Android : Insert multiple rows into sqlite database not working

查看:194
本文介绍了安卓:插入多行到SQLite数据库无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我插入一行到数据库中使用下面的方法进行的Andr​​oid项目。

I am Inserting single row into database using following method for android project.

myDB.execSQL("INSERT INTO "
 + Buss
 + " (BussName, RouteName)"
 + " VALUES ('buss1', 'buss2');");

它做工精细。我看到这个链接<一个href=\"http://stackoverflow.com/questions/1609637/is-it-possible-to-insert-multiple-rows-at-a-time-in-an-sqlite-database\">Inserting在SQLite数据库多行。我尝试这个方法(插入多行)在我的Andr​​oid项目,但它不能正常工作。

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需要一次分析查询 - 更多信息可以在这里找到:<一href=\"http://stackoverflow.com/questions/433392/how-do-i-use-$p$ppared-statements-in-sqlite-in-android\">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数据库无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆