现在的位置: 首页 > 综合 > 正文

使用contentProvider来进行SQlite transactions操作

2013年06月16日 ⁄ 综合 ⁄ 共 3844字 ⁄ 字号 评论关闭

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

 

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

Using SQLite Transactions with your ContentProvider

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.

01 public
class
YourProvider
extends
ContentProvider {
02 //..... define constants for the UriMatcher
03 public
static
final
int
EVENTS =
1
;
04 public
static
final
int
FESTIVITIES =
2
;
05 //....
06  
07 private
static
final
UriMatcher sUriMatcher = buildUriMatcher();
08 private
YourDatabase mOpenHelper;
09  
10 /**
11  
* Creates the Uri matcher
12  
*/
13 private
static
UriMatcher buildUriMatcher(){
14     
final
UriMatcher matcher =
new
UriMatcher(UriMatcher.NO_MATCH);
15     
final
String authority = YOUR_CONTENT_AUTHORITY;
16  
17    
//let the matcher match URIs with your defined constants
18     
matcher.addURI(authority,
"events"
, EVENTS);
19     
matcher.addURI(authority,
"festivities"
, FESTIVITIES);
20    
//.....
21  
22     
return
matcher;
23 }
24  
25 @Override
26 public
boolean
onCreate() {
27     
mOpenHelper =
new
YourDatabase(getContext());
28     
return
true
;
29 }
30  
31 @Override
32 public
String getType(Uri uri) {
33     
final
int
match = sUriMatcher.match(uri);
34     
switch
(match){
35     
case
EVENTS:
36         
return
EVENTS.CONTENT_TYPE;
37     
case
FESTIVITIES:
38         
return
FESTIVITIES.CONTENT_TYPE;
39     
//......
40     
default
:
41         
throw
new
UnsupportedOperationException(
"unknown: uri "
+ uri);
42     
}
43 }
44  
45 @Override
46 public
int
bulkInsert(Uri uri, ContentValues[] values) {
47     
final
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
48     
final
int
match = sUriMatcher.match(uri);
49     
switch
(match){
50     
case
EVENTS:
51                 
int
numInserted=
0
;
52         
db.beginTransaction();
53         
try
{
54             
//standard SQL insert statement, that can be reused
55             
SQLiteStatement insert =
56                 
db.compileStatement(
"insert into "
+ YOUR TABLE
57                         
+
"("
+ COLUMN1 +
","
+ COLUMN2
58                         
+
","
+ COLUMN3 +
")"
59                         
+
" values "
+
"(?,?,?"
);
60  
61             
for
(ContentValues value : values){
62                 
//bind the 1-indexed ?'s to the values specified
63                 
insert.bindString(
1
, value.getAsString(COLUMN1));
64                 
insert.bindString(
2
, value.getAsLong(COLUMN2));
65                 
insert.bindString(
3
, value.getAsString(COLUMN3));
66                 
insert.execute();
67             
}
68             
db.setTransactionSuccessful();
69             
numInserted = values.length
70         
}
finally
{
71             
db.endTransaction();
72         
}
73         
return
numInserted;
74     
//....
75     
default
:
76         
throw
new
UnsupportedOperationException(
"unsupported uri: "
+ uri);
77     
}}
78  
79 }

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.

抱歉!评论已关闭.