资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android中怎么批量插入数据

本篇文章为大家展示了Android中怎么批量插入数据,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

成都创新互联公司是一家专业提供承德县企业网站建设,专注与网站设计制作、成都网站建设、H5技术、小程序制作等业务。10年已为承德县众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

Android中在sqlite插入数据的时候默认一条语句就是一个事务(All   individual SQL Statements, (with rare exceptions like Bulk Inserts with  No Log, or Truncate Table) are automaticaly "In a Transaction" whether  you explicitly say so or not.. (even if they insert, update, or delete  millions of rows).),因此如果存在上万条数据插入的话,那就需要执行上万次插入操作,操作速度可想而知。因此在Android中插入数据时,使用批量插入的方式可以大大提高插入速度。

批量插入的模板如下:

public void inertOrUpdateDateBatch(List sqls) {           SQLiteDatabase db = getWritableDatabase();           db.beginTransaction();           try {               for (String sql : sqls) {                   db.execSQL(sql);               }               // 设置事务标志为成功,当结束事务时就会提交事务               db.setTransactionSuccessful();           } catch (Exception e) {               e.printStackTrace();           } finally {               // 结束事务               db.endTransaction();               db.close();           }       }

注意此处的:

db.execSQL(sql);

官方的API显示:

public void execSQL (String sql)

Added in API level 1

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

It has no means to return any data (such as the number of affected rows). Instead, you're encouraged to use insert(String,  String, ContentValues)update(String,  ContentValues, String, String[]), et al, when possible.

When using enableWriteAheadLogging(),  journal_mode is automatically managed by this class. So, do not set  journal_mode using "PRAGMA journal_mode'" statement if your app is  using enableWriteAheadLogging()

Parameters
sqlthe SQL statement to be executed. Multiple statements separated by semicolons are not supported.
Throws
SQLExceptionif the SQL string is invalid

说明,每次执行SQL只能有一条语句。在执行的时候,不能写成:

insert into student values('yang','boy');insert into student values('zhou','girl');

上述内容就是Android中怎么批量插入数据,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


文章题目:Android中怎么批量插入数据
文章位置:http://cdkjz.cn/article/gjgppc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220