首页 > 代码库 > Android -- Sqlite事务

Android -- Sqlite事务

这也是通过Android Juint来实现的。

基于上一次的工程继续,上一次工程传送门:《Android–Android Juint 与 Sqlite》 http://www.cnblogs.com/yydcdut/p/3790579.html

更新数据库                                                                                   

public PersonSQLite(Context context) {        super(context, "person.db", null, 2);        // TODO 自动生成的构造函数存根    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        // TODO 自动生成的方法存根        System.out.println("数据库发生了变化!!");        db.execSQL("alter table person add account varchar(20)");    }

SQL处理类                                                                                

public long addMoney(String name ,String number,int money)    {        SQLiteDatabase db = sqlite.getWritableDatabase();        //db.execSQL("insert into person (name,number) values (?,?)", new Object[]{name,number});        ContentValues values = new ContentValues();        values.put("name", name);        values.put("number", number);        values.put("account", money);        long id = db.insert("person", null, values);        db.close();        return id;    }

测试                                                                                          

//先添加数据    public void testAddMoney()    {        PersonDao2 dao = new PersonDao2(getContext());        dao.addMoney("wangwu", "123", 5000);        dao.addMoney("zhangsan", "321", 2000);    }    //事务处理    public void testTransaction()    {        PersonSQLite sqlite = new PersonSQLite(getContext());        SQLiteDatabase db = sqlite.getWritableDatabase();        db.beginTransaction();        db.execSQL("update person set account=account-1000 where name = ?",new Object[]{"zhangsan"});        db.execSQL("update person set account=account+1000 where name = ?",new Object[]{"wangwu"});        db.endTransaction();        db.close();    }

我是天王盖地虎的分割线                                                                 

源代码:http://pan.baidu.com/s/1dD1Qx01

SQLite2.zip

 

 

 

转载请注明出处:http://www.cnblogs.com/yydcdut