android 数据库事务处理简单介绍

来源:互联网 发布:初级php程序员面试 编辑:程序博客网 时间:2024/06/06 09:36

参照上篇日志的代码结构,添加该文件

TestTransaction.java

package com.demo.introductiontothedb.test;import com.demo.introductiontothedb.PersonDBOpenHelper;import android.database.sqlite.SQLiteDatabase;import android.test.AndroidTestCase;public class TestTransaction extends AndroidTestCase {public void testTran(){PersonDBOpenHelper helper = new PersonDBOpenHelper(getContext());SQLiteDatabase db = helper.getWritableDatabase();db.beginTransaction();try {db.execSQL("update personInfo set money='5000' where id = '20'");db.execSQL("update personInfo set money='9500' where id = '21'");db.setTransactionSuccessful();//只有设置了事务执行成功数据才会在endTransaction()的时候提交到数据库}catch(Exception e){}finally {db.endTransaction();}}}


0 0
原创粉丝点击