sqlite

5、使用事務操做SQLite數據庫web

使用SQLiteDatabase的beginTransaction()方法能夠開啓一個事務,程序執行到endTransaction() 方法時會檢查事務的標誌是否爲成功,若是程序執行到endTransaction()以前調用了setTransactionSuccessful() 方法設置事務的標誌爲成功則提交事務,若是沒有調用setTransactionSuccessful() 方法則回滾事務。數據庫

使用例子以下:spa

複製代碼

SQLiteDatabase db = ....;
db.beginTransaction();
//開始事務
try {
   db.execSQL(
"insert into person(name, age) values(?,?)", new Object[]{"林計欽", 4});
   db.execSQL(
"update person set name=? where personid=?", new Object[]{"abc", 1});
   db.setTransactionSuccessful();
//調用此方法會在執行到endTransaction() 時提交當前事務,若是不調用此方法會回滾事務
} finally {
   db.endTransaction();
//由事務的標誌決定是提交事務,仍是回滾事務
}
db.close();
code

複製代碼

上面兩條SQL語句在同一個事務中執行。orm

相關文章
相關標籤/搜索