使用contentProvider来进行SQlite transactions操作
项目需要在另外一个线程扫描数据并插入数据到数据库中,一条一条插的话时间太慢,于是考虑到用SQlite的事务来进行提交,但是ContentProvider并没有提供用于事务的API给上层,下面是我在网上找到的使用ContentProvider来进行SQlite transation的另一种方法。 原文地址: http://eshyu.wordpress.com/2010/08/15/using-sqlite-transactions-with-your-contentprovider/ Using SQLite Transactions with yourContentProviderAugust 15,2010In 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 ? |