数据库Dao层编增删改查写,数据库事务,数据库升级

数据库事务

有两个特点

1.安全性

情景:正常的转账行为,这个时候如果出现停电等异常,已经扣钱但是没有加钱;这个时候就可用数据库事务解决问题

2.高效性:

使用数据库事务添加享受同数量的数据,对比耗时少:

原理:没开始事务的是打开数据库,插入数据,关闭数据库:

  开启事务的是数据存到内存,然后一次写入到数据库;

数据库升级

升级的时候版本号必须要大于等于2;而且要大于上一版本;

package com.example.databasedemo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

public class DataBaseHelper extends SQLiteOpenHelper {

    private static final String TGA ="DatabaseHelper";

    public DataBaseHelper(@Nullable Context context) {
        super(context, Constants.DATABASE_NAME, null, Constants.VRESION_CODE);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建时回调
        Log.d(TGA,"创建数据库。。。");
        //创建字段
        //sql creat table table_name(
        String sql="create table "+Constants.TABLE_NAME+"(_id integer,name varchar,age integer,salary integer)";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //升级时回调
        Log.d(TGA,"升级数据库。。。");

        String sql;
        //db.execSQL(sql);

        switch (oldVersion)
        {
            case 1:

                sql="alert table "+Constants.TABLE_NAME+" add address varchar";
                db.execSQL(sql);
                break;

            case 2:
                sql="alert table "+Constants.TABLE_NAME+" add phone integer ,address varchar";
                db.execSQL(sql);
                break;


        }

    }
}

Dao层编写增删改查

与javaweb大体相同,只是代码有些不一样

package com.example.databasedemo;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.security.AccessControlContext;

public class Dao {

    private static final String TAG="Dao";
    private final DataBaseHelper mHelper;

    public Dao(AccessControlContext context){

         mHelper =new DataBaseHelper(context);
    }

    public void  insert(){
        SQLiteDatabase db= mHelper.getWritableDatabase();
        String sql ="insert into "+Constants.TABLE_NAME +" (_id ,name ,age,salary,phone,address) values (?,?,?,?,?,?)";
        db.execSQL(sql,new Object[]{1,"BillGates",60,1,110,"USA"});
        db.close();
    }

    public void delete(){
        SQLiteDatabase db= mHelper.getWritableDatabase();
        String sql ="delete  from  "+Constants.TABLE_NAME +" where age = 60";
        db.execSQL(sql);
        db.close();
    }

    public void update(){
        SQLiteDatabase db= mHelper.getWritableDatabase();
        String sql ="update  from  "+Constants.TABLE_NAME +" set salary= 2 where age = 60";
        db.execSQL(sql);
        db.close();
    }

    public void query(){

        SQLiteDatabase db= mHelper.getWritableDatabase();
        String sql ="select  form  "+Constants.TABLE_NAME +" ";
        Cursor cursor =db.rawQuery(sql,null);

        while (cursor.moveToNext()){
            int index=cursor.getColumnIndex("name");
            String name =cursor.getColumnName(index);
            Log.d(TAG,"name=="+name);
        }
        cursor.close();
        db.execSQL(sql);
        db.close();
    }
}

相关推荐