1.使用SQLite数据库的事务操作,编写一段模拟银行转账的逻辑代码。
1.使用SQLite数据库的事务操作,编写一段模拟银行转账的逻辑代码。
1.使用SQLite数据库的事务操作,编写一段模拟银行转账的逻辑代码。
PersonSQLiteOpenHelper helper = new PersonSQLiteOpenHelper (getApplication());
//获取一个可读写的SQLiteDataBase对象
SQLiteDatabase db = helper.getWritableDatabase();
// 开始数据库的事务
db.beginTransaction();
try {
//执行转出操作
db.execSQL("update person set account = account-1000 where name =?",
new Object] { "张三" });
//执行转入操作
db.execSQL("update information set account = account +1000 where name =?",
new Object] { "王五" });
//标记数据库事务执行成功
db.setTransactionSuccessful();
}catch (Exception e) {
Log.i("事务处理失败", e.toString());
} finally {
db.endTransaction(); //关闭事务
db.close(); //关闭数据库
}