写在前面
本文接上文的可持续化数据存储:https://www.cnblogs.com/wushenjiang/p/12541706.html,主要记录了Sqlite的用法
数据库的创建和升级
我们要创建Sqlite数据库,需要定义一个helper类继承SQLiteOpenHelper并复写其方法,如下代码示例:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
/**
*
* context 上下文
* name 数据库名称
* factory 游标工厂
* version 版本号
*/
public DatabaseHelper( Context context) {
super(context, Constants.DATABASE_NAME, null, Constants.VERSION_CODE);
}
/**
* 第一次创建数据库的时候被调用
* @param db
*/
@Override
public void onCreate(SQLiteDatabase db) {
//创建时的回调
Log.d(TAG,"创建数据库...");
//创建字段
// sql:create table table_name(_id integer,name varchar(50),age integer,salary integer);
String sql ="create table "+Constants.TABLE_NAME+"(_id integer,name varchar(50),age integer,salary integer);";
db.execSQL(sql);
}
升级数据库需要重写onUpgrade方法:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//升级数据库时的回调
Log.d(TAG,"升级数据库");
String sql;
// sql: alter table table_name add phone integer;
switch (oldVersion){
case 1:
//添加address和phone 字段
sql = "alter table " + Constants.TABLE_NAME + " add phone integer";
db.execSQL(sql);
sql = "alter table " + Constants.TABLE_NAME + " add address varchar";
db.execSQL(sql);
break;
case 2:
//添加address字段
sql = "alter table "+Constants.TABLE_NAME +" add address varchar";
db.execSQL(sql);
break;
case 3:
//添加新的表 account
sql = "create table account(_id interger,name varcahr,money interger)";
db.execSQL(sql);
break;
}
}
其中为了规范起见,把一些常量定义到常量类中了:
public class Constants {
public static final String DATABASE_NAME = "wushenjiang";
public static final int VERSION_CODE =3;
public static final String TABLE_NAME = "employee";
}
dao层的原始编写
在创建数据库后,我们就要开始编写dao类了。这里先使用最基本的方法来编写dao层
public Dao(Context 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,"zhangsan",18,1,"110","America"});*/
//添加数据
}
public void delete(){
SQLiteDatabase db = mHelper.getWritableDatabase();
/* String sql = "delete from "+Constants.TABLE_NAME+" where age = 18";
db.execSQL(sql);*/
}
public void update(){
SQLiteDatabase db = mHelper.getWritableDatabase();
/*String sql = "update "+Constants.TABLE_NAME+" set salary = 2 where age = 18";
db.execSQL(sql);*/
}
public void query(){
SQLiteDatabase db = mHelper.getWritableDatabase();
/* String sql = "select * from "+Constants.TABLE_NAME;
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
int index = cursor.getColumnIndex("name");
String name = cursor.getString(index);
Log.d(TAG,name);
cursor.close();*/
}
使用自带的API进行操作数据库
可以从上面看到,数据库操作很复杂,还需要写sql语句,十分麻烦。我们可以用API重写这些方法:
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,"zhangsan",18,1,"110","America"});*/
//添加数据
ContentValues values = new ContentValues();
values.put("_id",2);
values.put("name","barryAllen");
values.put("salary","1");
values.put("phone",1290);
values.put("address","valley");
values.put("age",30);
db.insert(Constants.TABLE_NAME,null,values);
db.close();
}
public void delete(){
SQLiteDatabase db = mHelper.getWritableDatabase();
/* String sql = "delete from "+Constants.TABLE_NAME+" where age = 18";
db.execSQL(sql);*/
int result = db.delete(Constants.TABLE_NAME, null, null);
Log.d(TAG,"result==" + result);
db.close();
}
public void update(){
SQLiteDatabase db = mHelper.getWritableDatabase();
/*String sql = "update "+Constants.TABLE_NAME+" set salary = 2 where age = 18";
db.execSQL(sql);*/
ContentValues values = new ContentValues();
values.put("phone","123456");
db.update(Constants.TABLE_NAME,values,null,null);
db.close();
}
public void query(){
SQLiteDatabase db = mHelper.getWritableDatabase();
/* String sql = "select * from "+Constants.TABLE_NAME;
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
int index = cursor.getColumnIndex("name");
String name = cursor.getString(index);
Log.d(TAG,name);
cursor.close();*/
Cursor cursor = db.query(Constants.TABLE_NAME,null,null,null,null,null,null,null);
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
Log.d(TAG,"id="+id+" name= "+name);
}
cursor.close();
db.close();
}
可以看到,使用API可以极大的简化我们的操作,并且更加安全。
数据库的事务操作
这块内容类似于Mysql里的事务操作。事务指的是将一堆操作绑定,如果没有全部完成就不算事务完成。典型的例子就是转账,如果转账中出现了异常,不使用事务的话就可能使得钱丢失。但用事务操作后钱就不会丢失了。且事务操作可以极大的提升效率。如下示例代码:
public void updateMoney(){
//在这里转账
SQLiteDatabase db = mHelper.getWritableDatabase();
//开启事务
db.beginTransaction();
try{
db.execSQL("update account set money = 1000000 - 12000 where name = 'company'");
//在这里发生异常
//int i= 10/0;
db.execSQL("update account set money = 12000 where name = 'myaccount'");
db.setTransactionSuccessful();
}catch (Exception e){
//处理异常
}finally {
//关闭事务
db.endTransaction();
db.close();
}
}