日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Android批量插入數(shù)據(jù)

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

批量插入的模板如下:

 
 
 
  1. public void inertOrUpdateDateBatch(List 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

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

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

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


當(dāng)前標(biāo)題:Android批量插入數(shù)據(jù)
標(biāo)題鏈接:http://m.5511xx.com/article/djjjddp.html