Using Transaction helpful in Android in my scenario?
I am performing two functionalities on my Application
i>Save data (Insertion of data which is quite large) ii> Data Deletion
I am using SQLite & creati开发者_如何学运维ng a single database object & referencing it to perform the different database operations. When the application is closing , I am closing the database object.
While saving & deleting the data , its taking approx 4-5 seconds .
I have the following questions:
i>Is opening & closing the database multiple times to handle the database queries a better approach or opening & closing the database only once?
ii>Will using transaction(read on some articles) , while inserting & deleting the data , help me in minimizing the processing time taken ?
If so , kindly provide some sample code/hints for transaction.
Thanks in advance.
Warm Regards,
CB
1) It depends on what are you trying to achieve. If you need to do database updates and update the UI respectively and that's an ongoing process for your application - better leave teh db connection open. If you just do some query to populate data once, or do some inserts - better is to close the database connection as soon as you're finished.
2) Transactions greatly improve speed. Sample code:
db.beginTransaction();
try {
// do your db operations here
...
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
精彩评论