android下的事物

来源:互联网 发布:mac版chrome没有flash 编辑:程序博客网 时间:2024/05/17 08:14
// 数据库的事务, 保证一组sql语句 要么同时执行成功 ,要么同时执行失败public void deteleAll() {// 删除数据库全部的记录.SQLiteDatabase db = helper.getWritableDatabase();db.beginTransaction();// 开启事务try {Cursor cursor = db.query("info", new String[] { "studentid" },null, null, null, null, null);while (cursor.moveToNext()) {String studentid = cursor.getString(0);db.delete("info", "studentid=?", new String[] { studentid });//throw new RuntimeException("--");}cursor.close();db.setTransactionSuccessful();//设置事务执行成功.必须要这一行代码执行,数据才会被提交} finally {db.endTransaction();//根据db设置的事务标记 来去确定事务是否被提交db.close();}}

0 0
原创粉丝点击