本文實(shí)例講述了Android數(shù)據(jù)庫中事務(wù)操作方法之銀行轉(zhuǎn)賬功能。分享給大家供大家參考,具體如下:
主java
package com.itheima.transtation;import com.itheima.transtation.db.BankOpenHelper;import android.os.Bundle;import android.app.Activity;import android.database.sqlite.SQLiteDatabase;import android.view.Menu;import android.view.View;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //點(diǎn)擊按鈕執(zhí)行該方法 public void transtation(View v){ //1.創(chuàng)建一個(gè)幫助類的對象 BankOpenHelper bankOpenHelper = new BankOpenHelper(this); //2.調(diào)用數(shù)據(jù)庫幫助類對象的getReadableDatabase創(chuàng)建數(shù)據(jù)庫,初始化表數(shù)據(jù),獲取一個(gè)SqliteDatabase對象去做轉(zhuǎn)賬(sql語句) SQLiteDatabase db = bankOpenHelper.getReadableDatabase(); //3.轉(zhuǎn)賬,將李四的錢減200,張三加200 db.beginTransaction();//開啟一個(gè)數(shù)據(jù)庫事務(wù) try { db.execSQL("update account set money= money-200 where name=?",new String[]{"李四"}); int i = 100/0;//模擬一個(gè)異常 db.execSQL("update account set money= money+200 where name=?",new String[]{"張三"}); db.setTransactionSuccessful();//標(biāo)記事務(wù)中的sql語句全部成功執(zhí)行 } finally { db.endTransaction();//判斷事務(wù)的標(biāo)記是否成功,如果不成功,回滾錯(cuò)誤之前執(zhí)行的sql語句 } }}最好自己創(chuàng)建一個(gè)包來寫數(shù)據(jù)庫類
package com.itheima.transtation.db;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class BankOpenHelper extends SQLiteOpenHelper { public BankOpenHelper(Context context) { super(context, "bank.db", null, 1); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table account (_id integer primary key autoincrement,name varchar(20),money varchar(20))"); db.execSQL("insert into account ('name','money') values ('張三','2000')"); db.execSQL("insert into account ('name','money') values ('李四','5000')"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub }}xml 一個(gè)按鈕的點(diǎn)擊事件,很特別
<?xml version="1.0"?>-<RelativeLayout tools:context=".MainActivity" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"><Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/transtation" android:onClick="transtation" android:layout_centerInParent="true"/></RelativeLayout>
附:
數(shù)據(jù)庫的事務(wù)說明:
事務(wù): 執(zhí)行多條sql語句,要么同時(shí)執(zhí)行成功,要么同時(shí)執(zhí)行失敗,不能有的成功,有的失敗
銀行轉(zhuǎn)賬
//點(diǎn)擊按鈕執(zhí)行該方法public void transtation(View v){ //1.創(chuàng)建一個(gè)幫助類的對象 BankOpenHelper bankOpenHelper = new BankOpenHelper(this); //2.調(diào)用數(shù)據(jù)庫幫助類對象的getReadableDatabase創(chuàng)建數(shù)據(jù)庫,初始化表數(shù)據(jù),獲取一個(gè)SqliteDatabase對象去做轉(zhuǎn)賬(sql語句) SQLiteDatabase db = bankOpenHelper.getReadableDatabase(); //3.轉(zhuǎn)賬,將李四的錢減200,張三加200 db.beginTransaction();//開啟一個(gè)數(shù)據(jù)庫事務(wù) try { db.execSQL("update account set money= money-200 where name=?",new String[]{"李四"}); int i = 100/0;//模擬一個(gè)異常 db.execSQL("update account set money= money+200 where name=?",new String[]{"張三"}); db.setTransactionSuccessful();//標(biāo)記事務(wù)中的sql語句全部成功執(zhí)行 } finally { db.endTransaction();//判斷事務(wù)的標(biāo)記是否成功,如果不成功,回滾錯(cuò)誤之前執(zhí)行的sql語句 }}希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注