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

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

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();
		}

	}

}


你可能感兴趣的:(Android基础)