自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android批量插入數(shù)據(jù)

移動(dòng)開發(fā) Android
本文通過全方位的介紹和講解為讀者朋友們呈現(xiàn)出了Android批量插入數(shù)據(jù)的實(shí)現(xiàn)方法,解決了以往的上萬次插入操作速度慢的問題,大大提高了插入速度。

Android中在sqlite插入數(shù)據(jù)的時(shí)候默認(rèn)一條語句就是一個(gè)事務(wù)(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).),因此如果存在上萬條數(shù)據(jù)插入的話,那就需要執(zhí)行上萬次插入操作,操作速度可想而知。因此在Android中插入數(shù)據(jù)時(shí),使用批量插入的方式可以大大提高插入速度。

批量插入的模板如下:

  1. public void inertOrUpdateDateBatch(List<String> sqls) {   
  2.         SQLiteDatabase db = getWritableDatabase();   
  3.         db.beginTransaction();   
  4.         try {   
  5.             for (String sql : sqls) {   
  6.                 db.execSQL(sql);   
  7.             }   
  8.             // 設(shè)置事務(wù)標(biāo)志為成功,當(dāng)結(jié)束事務(wù)時(shí)就會(huì)提交事務(wù)   
  9.             db.setTransactionSuccessful();   
  10.         } catch (Exception e) {   
  11.             e.printStackTrace();   
  12.         } finally {   
  13.             // 結(jié)束事務(wù)   
  14.             db.endTransaction();   
  15.             db.close();   
  16.         }   
  17.     } 

注意此處的:

  1. 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
sql the SQL statement to be executed. Multiple statements separated by semicolons are not supported.
Throws
SQLException if the SQL string is invalid

說明,每次執(zhí)行SQL只能有一條語句。在執(zhí)行的時(shí)候,不能寫成:

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

形式,而需要將兩條SQL語句拆開,每條SQL語句執(zhí)行一次。

責(zé)任編輯:閆佳明 來源: csdn
相關(guān)推薦

2021-04-08 10:55:53

MySQL數(shù)據(jù)庫代碼

2011-08-04 18:00:47

SQLite數(shù)據(jù)庫批量數(shù)據(jù)

2021-02-01 00:04:13

Dictionary數(shù)據(jù)批量

2024-10-22 08:47:03

2021-09-27 07:56:41

MyBatis Plu數(shù)據(jù)庫批量插入

2010-09-03 11:47:38

SQL刪除

2010-09-01 16:26:11

SQL刪除批量

2022-09-29 10:06:56

SQLMySQL服務(wù)端

2010-09-08 16:53:43

SQL查詢循環(huán)

2025-04-07 03:00:00

SpringBoot數(shù)據(jù)庫

2021-10-09 06:59:36

技術(shù)MyBatis數(shù)據(jù)

2023-12-30 20:04:51

MyBatis框架數(shù)據(jù)

2009-07-20 17:03:55

批量插入數(shù)據(jù)ASP.NET

2020-11-23 10:50:27

MySQLSQL數(shù)據(jù)庫

2013-09-22 10:25:23

MySQLSQL性能優(yōu)化

2018-08-09 08:59:56

數(shù)據(jù)庫MySQL性能優(yōu)化

2022-09-23 09:44:17

MyBatisforeach

2021-06-28 10:25:47

MySQL數(shù)據(jù)庫重復(fù)數(shù)據(jù)

2021-09-14 13:15:43

MySQL數(shù)據(jù)庫腳本

2024-07-31 09:56:20

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)