数据库事务

有两个特点

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();
}
}

数据库Dao层编增删改查写,数据库事务,数据库升级的更多相关文章

  1. Delphi - cxGrid连接Oracle数据库 实现数据的增删改查

    cxGrid连接Oracle数据库 实现数据的增删改查 cxGrid连接Oracle数据库 1:通过OraSession连接数据库.OraDataSet实现OraSession和OraDataSour ...

  2. sqlite数据库操作详细介绍 增删改查,游标

    sqlite数据库操作详细介绍 增删改查,游标     本文来源于www.ifyao.com禁止转载!www.ifyao.com Source code     package com.example ...

  3. 【设计模式】【应用】使用模板方法设计模式、策略模式 处理DAO中的增删改查

    原文:使用模板方法设计模式.策略模式 处理DAO中的增删改查 关于模板模式和策略模式参考前面的文章. 分析 在dao中,我们经常要做增删改查操作,如果每个对每个业务对象的操作都写一遍,代码量非常庞大. ...

  4. abp(net core)+easyui+efcore仓储系统——展现层实现增删改查之控制器(六)

    abp(net core)+easyui+efcore仓储系统目录 abp(net core)+easyui+efcore仓储系统——ABP总体介绍(一) abp(net core)+easyui+e ...

  5. MySQL数据库之表的增删改查

    目录 MySQL数据库之表的增删改查 1 引言 2 创建表 3 删除表 4 修改表 5 查看表 6 复制表 MySQL数据库之表的增删改查 1 引言 1.MySQL数据库中,数据库database就是 ...

  6. 在python中连接mysql数据库,并进行增删改查

    数据库在开发过程中是最常见的,基本上在服务端的编程过程中都会使用到,mysql是较常见的一种数据库,这里介绍python如果连接到数据库中,并对数据库进行增删改查. 安装mysql的python扩展 ...

  7. ABP入门系列(6)——展现层实现增删改查

    这一章节将通过完善Controller.View.ViewModel,来实现展现层的增删改查.最终实现效果如下图: 一.定义Controller ABP对ASP.NET MVC Controllers ...

  8. ABP入门系列(5)——展现层实现增删改查

    ABP入门系列目录--学习Abp框架之实操演练 这一章节将通过完善Controller.View.ViewModel,来实现展现层的增删改查.最终实现效果如下图: 一.定义Controller ABP ...

  9. abp(net core)+easyui+efcore实现仓储管理系统——展现层实现增删改查之列表视图(七)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

随机推荐

  1. 基于tensorflow2.0和cifar100的VGG13网络训练

    VGG是2014年ILSVRC图像分类竞赛的第二名,相比当年的冠军GoogleNet在可扩展性方面更胜一筹,此外,它也是从图像中提取特征的CNN首选算法,VGG的各种网络模型结构如下: 今天代码的原型 ...

  2. asp.net mvc 使用AuthorizeAttribute做授权验证

    授权验证,比如登陆验证 1.自定义属性继承AuthorizeAttribute 2.重写OnAuthorization方法 3.通过AllowAnonymousAttribute特性处理无需授权的Ac ...

  3. JMeter性能监控插件PerfMon Metrics Collector

    Jmeter性能监控插件由客户端插件和服务器端程序组成. 官方文档及插件下载地址https://jmeter-plugins.org/wiki/PerfMon/ 将插件 plugins-manager ...

  4. MySQL 当记录不存在时插入(insert if not exists、dual )

    INSERT INTO clients(client_id, client_name, client_type)SELECT 10345, ’IBM’, ’advertising’FROM dualW ...

  5. windows服务踩的坑

    最近写了一个windows服务 有一些bug最后终于解决了还是写点经验把. 第一点.版本问题,因为是小白,第一次写windows服务,选择的是.net4.6.1的目标框架,因为我的电脑是windows ...

  6. Git 的 .gitignore 配置说明 (C#)

    1.配置语法: 以斜杠“/”开头表示目录: 以星号“*”通配多个字符: 以问号“?”通配单个字符 以方括号“[]”包含单个字符的匹配列表: 以叹号“!”表示不忽略(跟踪)匹配到的文件或目录: 此外,g ...

  7. python 复习 day1

    import timeimport json # 二:嵌套取值操作students_info=[['egon',18,['play',]],['alex',18,['play','sleep']]] ...

  8. nginx: [warn] conflicting server name "aaa.7yule.cn" on 0.0.0.0:80, ignored

    故障现象: 修改nginx配置参数后,使用nginx -t检查配置,出现告警提示 nginx: [warn] conflicting server name "aaa.7yule.cn&qu ...

  9. npm 安装与部署

    nodejs 安装 查看版本 官方网址 下载linux版,64位 wget https://npm.taobao.org/mirrors/node/v12.13.1/node-v12.13.1-lin ...

  10. Node.js核心模块-fs文件系统

    fs是file-system的简写,文件系统的意思.在Node中如果想要进行文件操作,就必须引入fs这个核心模块. 引入 const fs = require('fs') fs.readFile(pa ...