Android开发入门之在SQLite中使用事务
来源:互联网 发布:淘宝标题什么是长尾词 编辑:程序博客网 时间:2024/05/18 00:32
银行转账
1)update person set amount=amount-10 where personid=1;
2)update person set amount=amount+10 where personid=2;
以上个例子为基础,修改DBOpenHelper类,增加amount字段升级数据库
package cn.leigo.service;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class DBOpenHelper extends SQLiteOpenHelper {public DBOpenHelper(Context context) {super(context, "leigo.db", null, 3); //<包>/databases/}@Overridepublic void onCreate(SQLiteDatabase db) {// 数据库第一次被创建的时候调用的db.execSQL("CREATE TABLE person (personid integer primary key autoincrement, name varchar(20))");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL("ALTER TABLE person ADD amount integer");}}
在Person类中增加amount变量:
package cn.leigo.domain;public class Person {private Integer id;private String name;private String phone;private Integer amount;public Person() {}public Person(String name, String phone, Integer amount) {this.name = name;this.phone = phone;this.amount = amount;}public Person(Integer id, String name, String phone, Integer amount) {this.id = id;this.name = name;this.phone = phone;this.amount = amount;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public Integer getAmount() {return amount;}public void setAmount(Integer amount) {this.amount = amount;}@Overridepublic String toString() {return "Person [id=" + id + ", name=" + name + ", phone=" + phone+ ", amount=" + amount + "]";}}
修改报错的类
更新数据
public void testUpdateAmount() throws Exception {PersonService service = new PersonService(getContext());Person person1 = service.find(1);Person person2 = service.find(2);person1.setAmount(100);person2.setAmount(50);service.update(person1);service.update(person2);}
public void payment() {SQLiteDatabase db = dbOpenHelper.getWritableDatabase();// 开启事务db.beginTransaction();db.execSQL("UPDATE person SET amount=amount-10 where personid=1");//System.out.println(1/0);db.execSQL("UPDATE person SET amount=amount+10 where personid=2");db.setTransactionSuccessful();// 结束事务,有两种情况:commit、rollback// 事务的提交或者回滚是由事务的标识决定的,如果事务的标识为true,事务就会提交否则回滚,默认情况下事务的标识为falsedb.endTransaction();db.close();}
public void testPayment() throws Exception {PersonService service = new PersonService(getContext());service.payment();}
将
System.out.println(1/0);前的注释去掉,重新测试,测试失败,数据未修改。
- Android开发入门之在SQLite中使用事务
- Android在SQLite中使用事务
- 在Android SQLite中使用事务
- Android在SQLite中使用事务
- (Android)在SQLite中使用事务
- Android在SQLite中使用事务
- Android在SQLite中使用事务
- 在SQLite中使用事务
- 在SQLite中使用事务
- 在SQLite中使用事务
- 在SQLite中使用事务
- 在SQLite中使用事务
- Android中SQLite使用事务
- Android学习笔记----18_在SQLite中使用事务
- 一看就会Android之SQLite中事务的使用
- 实例教程七:在SQLite中使用事务
- 在SQLite中如何使用事务
- Android Sqlite数据库之使用事务
- 关于ios访问HTTPS站点的几种方法
- 文件(夹)操作SHFileOperation (删除 复制 移动) 报错 无法读取源文件或磁盘
- dlopen
- Jquery 常用方法
- 获取 ACE 主动对象方法返回值的另外一种方法
- Android开发入门之在SQLite中使用事务
- PLSQL Developer连接不上64位Oracle 10g的解决办法
- linux expect自动登录ssh,ftp
- 【全方位检查Windows系统变慢的问题】
- Cocos2d-X游戏开发之CCTouchDelegate
- springSecurity源码分析——DelegatingFilterProxy类的作用
- ini文件
- Nginx学习之九-负载均衡(客户端请求与Nginx进程间负载均衡)
- Windows环境下32位汇编语言程序设计(典藏版)