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

本篇文章為大家展示了Android中怎么批量插入數(shù)據(jù),內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供承德縣企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為承德縣眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jì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í),使用批量插入的方式可以大大提高插入速度。

批量插入的模板如下:

public void inertOrUpdateDateBatch(List<String> sqls) {           SQLiteDatabase db = getWritableDatabase();           db.beginTransaction();           try {               for (String sql : sqls) {                   db.execSQL(sql);               }               // 設(shè)置事務(wù)標(biāo)志為成功,當(dāng)結(jié)束事務(wù)時(shí)就會提交事務(wù)               db.setTransactionSuccessful();           } catch (Exception e) {               e.printStackTrace();           } finally {               // 結(jié)束事務(wù)               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

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

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

上述內(nèi)容就是Android中怎么批量插入數(shù)據(jù),你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

名稱欄目:Android中怎么批量插入數(shù)據(jù)
URL網(wǎng)址:http://www.muchs.cn/article32/gjgppc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、小程序開發(fā)網(wǎng)站策劃、用戶體驗(yàn)定制網(wǎng)站、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)