安卓:插入多行到SQLite数据库无法正常工作

编程入门 行业动态 更新时间:2024-10-26 22:29:23
本文介绍了安卓:插入多行到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');");

它做工精细。我看到这个链接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需要一次分析查询 - 更多信息可以在这里找到: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数据库无法正常工作

本文发布于:2023-11-27 10:04:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   数据库   工作   多行到   SQLite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!