Android-事务(Transaction)


事务就拿转帐的例子来说:两个用户,要么都成功,要么都失败,这样才是安全🔐的,可以保证两方或者多方,全部成功才成功,否则就回滚(不会对数据有任何修改)

 

定义数据库与表

package liudeli.datastorage.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQLiteOpenHelper2 extends SQLiteOpenHelper {

    public static MySQLiteOpenHelper2 mySQLiteOpenHelper;

    /**
     * 由于表名每次使用很频繁,所有定义成常量
     */
    public static final String TABLE_NAME = "person_table";

    private static final String DB_NAME = "csbank.db";
    private static final int VERSION = 1;

    public synchronized static MySQLiteOpenHelper2 getInstance(Context context) {
        if (null == mySQLiteOpenHelper) {
            mySQLiteOpenHelper = new MySQLiteOpenHelper2(context, DB_NAME, null, VERSION);
        }
        return mySQLiteOpenHelper;
    }

    /**
     * 当开发者调用 getReadableDatabase(); 或者 getWritableDatabase();
     * 就会通过此构造方法配置的信息 来创建 person_info.db 数据库
     * 此方法的另外作用是,如果存着数据库就打开数据库,不存着数据库就创建数据库
     * @param context 上下文
     * @param name    数据库名
     * @param factory 游标工厂
     * @param version 版本,最低为1
     */
    private MySQLiteOpenHelper2(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    /**
     * 此方法是何时调用? ,是需要开发者调用 getReadableDatabase(); 或者 getWritableDatabase();
     * 此方法的作用是,如果没有表就创建打开,如果有表就打开
     * @param db 可执行SQL语句
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table "+TABLE_NAME+"(_id integer primary key autoincrement, name text, age integer, my_assets text);");

        ContentValues values = new ContentValues();
        values.put("name", "张三");
        values.put("age", 62);
        values.put("my_assets", "1000000");
        db.insert(TABLE_NAME, null, values);

        values.put("name", "李四");
        values.put("age", 22);
        values.put("my_assets", "2000");
        db.insert(TABLE_NAME, null, values);
    }

    /**
     * 此方法用于数据库升级
     * @param db 可执行SQL语句
     * @param oldVersion 以前旧版本的版本号
     * @param newVersion 现在目前最新的版本号
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

 

事务的实际操作:

package liudeli.datastorage;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import liudeli.datastorage.db.MySQLiteOpenHelper2;

public class TransactionActivity extends Activity {

    private final String TAG = TransactionActivity.class.getSimpleName();

    MySQLiteOpenHelper2 dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transaction);

        dbHelper = MySQLiteOpenHelper2.getInstance(this);
    }

    public void queryALl(View view) {
        query(null); // 查询全部
    }

    private void query(String orderBy) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(MySQLiteOpenHelper2.TABLE_NAME,
                new String[]{"*"},
                null,
                null,
                null,
                null,
                orderBy);
        while (cursor.moveToNext()) {
            int _id = cursor.getInt(0);
            String name = cursor.getString(1);
            int age = cursor.getInt(2);
            String myAssets = cursor.getString(3);
            Log.d(TAG, " id:" + _id + " name:" + name + " age" + age + " my_assets:" + myAssets);
        }
        cursor.close();
        db.close();
    }

    /**
     * 转账
     * @param view
     */
    public void moveMoney(View view) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            int moneyValue1 = 1000000;
            int moneyValue2 = 2000;

            db.beginTransaction(); // 开启事务

            ContentValues contentValues = new ContentValues();
            contentValues.put("my_assets", moneyValue1 - 6000);
            int result1 = db.update(MySQLiteOpenHelper2.TABLE_NAME, contentValues, "_id = ?", new String[]{"1"});

            contentValues.put("my_assets", moneyValue2 + 6000);
            int result2 = db.update(MySQLiteOpenHelper2.TABLE_NAME, contentValues, "_id = ?", new String[]{"2"});

            if (result1>0 && result2>0) {
                db.setTransactionSuccessful(); // 事务默认是失败的,要设置成功,否则数据不会修改
                Toast.makeText(TransactionActivity.this, "转帐成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(TransactionActivity.this, "转帐失败", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(TransactionActivity.this, "转帐失败", Toast.LENGTH_SHORT).show();

        } finally {

            db.endTransaction(); // 注意⚠️:一定要结束事务,不然查询会报错

            db.close();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

 

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="转账"
        android:onClick="moveMoney"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="queryALl"
        android:text="查询"
        android:layout_alignParentRight="true"
        />

</RelativeLayout>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM