请在 下方输入 要搜索的题目:

1.使用SQLite数据库的事务操作,编写一段模拟银行转账的逻辑代码。

1.使用SQLite数据库的事务操作,编写一段模拟银行转账的逻辑代码。

发布时间:2025-06-27 12:32:50
推荐参考答案 ( 由 快搜搜题库 官方老师解答 )
联系客服
答案:

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();              //关闭数据库

}

专业技术学习
专业技术学习
搜搜题库系统