Application initialization requires a lot of data to be injected into the SQLite, and a separate use of the For+insert method causes the application to respond slowly because the default statement of the SQLite when inserting data is a transaction and how many times disk operations there are. The first 5,000 records of my application are 5,000 read and write disk operations.
And there is no guarantee that all data can be plugged in at the same time. (It is possible that part of the insert succeeds, the other part fails, and the follow-up is removed.) Too much trouble)
Workaround:
Add transaction, insert 5,000 as a transaction
We use SQLite transactions for control:
code as follows:
Db.begintransaction (); Set Start transaction manually
try{
Bulk processing operations
for (Collection c:colls) {
Insert (db, C);
}
Db.settransactionsuccessful (); Set transaction success, no setting will automatically rollback uncommitted.
No database operations between Settransactionsuccessful and Endtransaction
}catch (Exception e) {
Mylog.printstacktracestring (e);
}finally{
Db.endtransaction (); Processing complete
}
One,Use the Sqlitedatabase BeginTransaction () method to open a transaction that checks whether the transaction's flag is successful if the program executes to the Endtransaction () method, if the program executes to Endtransaction () The Settransactionsuccessful () method was previously invoked to set the transaction's flag as successful, then all operations starting from BeginTransaction () would be committed if the settransactionsuccessful () was not invoked Method rolls back the transaction.
Second, the use of examples are as follows: The following two SQL statements are executed in the same transaction.
Java code
Bank account Transaction Test
public void Payment ()
{
Sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
Open transaction
Db.begintransaction ();
Try
{
Db.execsql ("Update person set amount=amount-10 where personid=?", New Object[]{1});
Db.execsql ("Update person set amount=amount+10 where personid=?", New object[]{2});
Set the transaction flag to succeed and commit the transaction when the transaction is closed
Db.settransactionsuccessful ();
}
catch (Exception e) {
Throw (e);
}
Finally
{
End transaction
Db.endtransaction ();
}
}