加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用contentProvider来进行SQlite transactions操作

发布时间:2020-12-12 20:43:38 所属栏目:百科 来源:网络整理
导读:项目需要在另外一个线程扫描数据并插入数据到数据库中,一条一条插的话时间太慢,于是考虑到用SQlite的事务来进行提交,但是ContentProvider并没有提供用于事务的API给上层,下面是我在网上找到的使用ContentProvider来进行SQlite transation的另一种方法。
01publicclassYourProvider extendsContentProvider {02//..... define constants for the UriMatcher03publicstaticfinalintEVENTS = 1;04publicstaticfinalintFESTIVITIES = 2;05//....0607privatestaticfinalUriMatcher sUriMatcher = buildUriMatcher();08privateYourDatabase mOpenHelper;0910/**11* Creates the Uri matcher12*/13privatestaticUriMatcher buildUriMatcher(){14finalUriMatcher matcher = newUriMatcher(UriMatcher.NO_MATCH);15finalString authority = YOUR_CONTENT_AUTHORITY;1617//let the matcher match URIs with your defined constants18matcher.addURI(authority,"events",EVENTS);19matcher.addURI(authority,"festivities",FESTIVITIES);20//.....2122returnmatcher;23}2425@Override26publicbooleanonCreate() {27mOpenHelper = newYourDatabase(getContext());28returntrue;29}3031@Override32publicString getType(Uri uri) {33finalintmatch = sUriMatcher.match(uri);34switch(match){35caseEVENTS:36returnEVENTS.CONTENT_TYPE;37caseFESTIVITIES:38returnFESTIVITIES.CONTENT_TYPE;39//......40default:41thrownewUnsupportedOperationException("unknown: uri "+ uri);42}43}4445@Override46publicintbulkInsert(Uri uri,ContentValues[] values) {47finalSQLiteDatabase db = mOpenHelper.getWritableDatabase();48finalintmatch = sUriMatcher.match(uri);49switch(match){50caseEVENTS:51intnumInserted= 0;52db.beginTransaction();53try{54//standard SQL insert statement,that can be reused55SQLiteStatement insert =56db.compileStatement("insert into "+ YOUR TABLE57+ "("+ COLUMN1 + ","+ COLUMN258+ ","+ COLUMN3 + ")"59+" values "+ "(?,?,?");6061for(ContentValues value : values){62//bind the 1-indexed ?'s to the values specified63insert.bindString(1,value.getAsString(COLUMN1));64insert.bindString(2,value.getAsLong(COLUMN2));65insert.bindString(3,value.getAsString(COLUMN3));66insert.execute();67}68db.setTransactionSuccessful();69numInserted = values.length70} finally{71db.endTransaction();72}73returnnumInserted;74//....75default:76thrownewUnsupportedOperationException("unsupported uri: "+ uri);77}}7879}

So,for each ContentURI you are suppporting (each table in your db),write its respective bulkInsert case as above. And now you will witness an absolutely HUGE increase in performance (for me it cut the bulkInsert() time from 20 seconds to 1),and the return value of the bulkInsert() will now let you know if the transaction was successful or not. Also look here to see the transaction API.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

项目需要在另外一个线程扫描数据并插入数据到数据库中,一条一条插的话时间太慢,于是考虑到用SQlite的事务来进行提交,但是ContentProvider并没有提供用于事务的API给上层,下面是我在网上找到的使用ContentProvider来进行SQlite transation的另一种方法。

原文地址: http://eshyu.wordpress.com/2010/08/15/using-sqlite-transactions-with-your-contentprovider/

Using SQLite Transactions with yourContentProvider

Posted on by eshyu

In the world of databases,a transaction is a unit of work (including insertions,deletions,updates) that is Atomic,Consistent,Isolated,and Durable. By default in SQLite,each insertion is a transaction. And to preserve data integrity,SQLite will wait until data is stored on the disk before completing the transaction. So if you have a large data set that you’re trying to insert into your database,inserting each piece individually is going to seem extremely slow.

You want to use transactions,not just because they will increase the performance of your database operations. Because transactions are atomic,they will help you ensure your database is consistent. For example,if you need to process a large batch of instructions,then either everything happened correctly,or if something went wrong then that whole transaction failed (so it’s all or nothing).

By default the ContentResolver API provides a bulkInsert() method,but its not atomic and its slow as hell,so let’s override the bulkInsert() method in our ContentProvider.

view source print ?
    推荐文章
      热点阅读