Android:数据库SQLite事务处理

使用事务可以让一系列操作要不全部成功执行,要么全部不执行:

db.beginTransaction();  //开启事务
try{
    db.execSQL("delete from book where id=?", new String[]{"3"});
    if(true){
        throw new NullPointerException(); 
    }
    db.execSQL("insert into book (name, author, price, pages) values (?, ?, ?, ?)", 
        new String[]{"name", "author", "9.99", "234"});
}catch(Exception ex){
    ex.printStackTrace();
}finally{
    db.endTransaction();    //结束事务
}

以上代码由于使用事务机制,所以删除语句也不会执行。

posted @ 2019-01-03 14:07  xuejianbest  阅读(363)  评论(0编辑  收藏  举报