In the Android development process, often to use SQLite to store some data, this application should be more common, but sometimes unavoidably inadvertently, will appear sqlite create table success, insert not error, but no data inserted.
For specific questions, see the following code:
Context CTX;
Sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
Open transaction
Db.begintransaction ();
try{
Database operations
For (...) {
Inserting data
Db.insert (table, values);
}
//SET transaction flag to be successful, commit transaction when end transaction
//(Be sure to add this sentence, otherwise the insert succeeds, but the database has no data)
db.settransactionsuccessful ();
}
catch (Exception ex) {
} finally {
//End Transaction method checks whether a transaction's flag is successful,
//If the program executes this method before calling the Settransactionsuccessful () method to set the transaction flag to successful then commit the transaction
//If the Settransactionsuccessful () method is not called, the transaction is rolled back.
db.endtransaction ();
}
When Android was developed, SQLite created the table successfully, insert does not error, but there is no reason for data insertion