标签:增删改查 port var led val roi demo 问题 升级
数据库事务
有两个特点
1.安全性
情景:正常的转账行为,这个时候如果出现停电等异常,已经扣钱但是没有加钱;这个时候就可用数据库事务解决问题
2.高效性:
使用数据库事务添加享受同数量的数据,对比耗时少:
原理:没开始事务的是打开数据库,插入数据,关闭数据库:
开启事务的是数据存到内存,然后一次写入到数据库;
数据库升级
升级的时候版本号必须要大于等于2;而且要大于上一版本;
1 package com.example.databasedemo; 2 3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteOpenHelper; 6 import android.util.Log; 7 8 import androidx.annotation.Nullable; 9 10 public class DataBaseHelper extends SQLiteOpenHelper { 11 12 private static final String TGA ="DatabaseHelper"; 13 14 public DataBaseHelper(@Nullable Context context) { 15 super(context, Constants.DATABASE_NAME, null, Constants.VRESION_CODE); 16 } 17 18 @Override 19 public void onCreate(SQLiteDatabase db) { 20 //创建时回调 21 Log.d(TGA,"创建数据库。。。"); 22 //创建字段 23 //sql creat table table_name( 24 String sql="create table "+Constants.TABLE_NAME+"(_id integer,name varchar,age integer,salary integer)"; 25 db.execSQL(sql); 26 } 27 28 @Override 29 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 30 //升级时回调 31 Log.d(TGA,"升级数据库。。。"); 32 33 String sql; 34 //db.execSQL(sql); 35 36 switch (oldVersion) 37 { 38 case 1: 39 40 sql="alert table "+Constants.TABLE_NAME+" add address varchar"; 41 db.execSQL(sql); 42 break; 43 44 case 2: 45 sql="alert table "+Constants.TABLE_NAME+" add phone integer ,address varchar"; 46 db.execSQL(sql); 47 break; 48 49 50 } 51 52 } 53 }
Dao层编写增删改查
与javaweb大体相同,只是代码有些不一样
1 package com.example.databasedemo; 2 3 import android.content.Context; 4 import android.database.Cursor; 5 import android.database.sqlite.SQLiteDatabase; 6 import android.util.Log; 7 8 import java.security.AccessControlContext; 9 10 public class Dao { 11 12 private static final String TAG="Dao"; 13 private final DataBaseHelper mHelper; 14 15 public Dao(AccessControlContext context){ 16 17 mHelper =new DataBaseHelper(context); 18 } 19 20 public void insert(){ 21 SQLiteDatabase db= mHelper.getWritableDatabase(); 22 String sql ="insert into "+Constants.TABLE_NAME +" (_id ,name ,age,salary,phone,address) values (?,?,?,?,?,?)"; 23 db.execSQL(sql,new Object[]{1,"BillGates",60,1,110,"USA"}); 24 db.close(); 25 } 26 27 public void delete(){ 28 SQLiteDatabase db= mHelper.getWritableDatabase(); 29 String sql ="delete from "+Constants.TABLE_NAME +" where age = 60"; 30 db.execSQL(sql); 31 db.close(); 32 } 33 34 public void update(){ 35 SQLiteDatabase db= mHelper.getWritableDatabase(); 36 String sql ="update from "+Constants.TABLE_NAME +" set salary= 2 where age = 60"; 37 db.execSQL(sql); 38 db.close(); 39 } 40 41 public void query(){ 42 43 SQLiteDatabase db= mHelper.getWritableDatabase(); 44 String sql ="select form "+Constants.TABLE_NAME +" "; 45 Cursor cursor =db.rawQuery(sql,null); 46 47 while (cursor.moveToNext()){ 48 int index=cursor.getColumnIndex("name"); 49 String name =cursor.getColumnName(index); 50 Log.d(TAG,"name=="+name); 51 } 52 cursor.close(); 53 db.execSQL(sql); 54 db.close(); 55 } 56 }
标签:增删改查 port var led val roi demo 问题 升级
原文地址:https://www.cnblogs.com/2940500426yingxin/p/12304926.html