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<String> 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 voidexecSQL(Stringsql)

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 useinsert(String, String, ContentValues),update(String, ContentValues, String, String[]), et al, when possible.

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

Parameters
sql the SQL statement to be executed. Multiple statements separated by semicolons are not supported.
Throws
SQLException if the SQL string is invalid
说明,每次执行SQL只能有一条语句。在执行的时候,不能写成:

insert into student values('yang','boy');insert into student values('zhou','girl');
形式,而需要将两条 SQL语句拆开,每条SQL语句执行一次。



更多相关文章

  1. 关于Android中的SQLite使用
  2. android sqlite和listview 使用小例子
  3. Android(安卓)封装的数据库管理操作类
  4. android p 充拔电提示音
  5. Android(安卓)Sqlite数据库中判断某个表是否存在的SQL语句
  6. android文档开发规范
  7. Android(安卓)之采用execSQL和rawQuery方法完成数据的添删改查操
  8. Android消息机制---MessageQueue的工作原理
  9. android之sqlite用法

随机推荐

  1. android中常见的设计模式有哪些?
  2. Android双向滑动菜单完全解析,教你如何一
  3. Android(安卓)ListView和Fragment结合使
  4. 最新Android简单实现省市区三级联动
  5. Android(安卓)WebView 开发中遇到的那些
  6. android 开机启动加速
  7. Android(安卓)调用系统相机以及相册源码
  8. 6月Android上网使用率已超越WM智能手机
  9. Android自定义滚动式时间选择器(在他人基
  10. android studio 中使用aidl