最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android數(shù)據(jù)庫中事務(wù)操作方法之銀行轉(zhuǎn)賬示例

 更新時(shí)間:2017年08月25日 15:10:27   作者:Qi_Yuan  
這篇文章主要介紹了Android數(shù)據(jù)庫中事務(wù)操作方法之銀行轉(zhuǎn)賬,以具體的銀行轉(zhuǎn)賬為例分析了Android數(shù)據(jù)庫操作中事務(wù)的使用與回滾相關(guān)操作技巧,需要的朋友可以參考下

本文實(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語句
 }
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論

达日县| 阜南县| 西乡县| 拉萨市| 舒兰市| 陆河县| 凉城县| 亳州市| 资源县| 凌源市| 奉贤区| 宝丰县| 青神县| 卢氏县| 平安县| 凌海市| 抚宁县| 石泉县| 赫章县| 泌阳县| 咸阳市| 保山市| 岐山县| 萍乡市| 嘉禾县| 铜陵市| 浙江省| 锦州市| 儋州市| 五指山市| 镇平县| 石楼县| 无棣县| 徐汇区| 桓台县| 九龙坡区| 重庆市| 金秀| 淮阳县| 宁明县| 安丘市|