您的位置:首页 > 移动开发 > Android开发

ormlite 框架对数据的版本升级

2017-01-21 11:22 288 查看

ormlite 框架对数据的版本升级

本文属于原创,请尊重笔者劳动成功(其实我很懒)

本文只对ormlite 框架对数据的版本升级做一些自己额阐述

其他相关对于ormlite框架的了解的话请参考

http://blog.csdn.net/lmj623565791/article/details/39122981

Android 本地数据库又叫SQLite,它有自己的一些原则:

1. 我们只能重命名据据库中的表名和向旧表中增加新的字段

2. Android版SQLite不支持修改表中的字段名和删除表中的字段,也不允许改变表中字段的约束条件(但是修改也可以通过增加来实现,我这里叫它打引号的’修改’)

3. Android版SQLite在存储字段数据时不分类型,所以可以改变字段的类型而无需要升级

(参考地址:http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_4.html#Upgrading-Schema)

对于原生sqlite数据库升级的操作,请参考最下面的git地址,在testdb包下有描述

废话不多说直接上代码(代码最新的所以跟结果截图会有些出入):

package com.myframe.www.testormlite;

import android.content.Context;
import android.content.pm.*;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import www.wuhai.common.utils.L;

/**
* Created by wuhai on 2017/01/18 16:18.
* 描述:
*/

public class DBHelper extends OrmLiteSqliteOpenHelper{

/**
* 数据库名字
*/
private static final String DB_NAME = "test.db";
/**
* 数据库版本
*/
private static final int VERSION = 3;
/**
* 用来存放Dao的map
*/
private Map<String, Dao> daos = new HashMap<String, Dao>();

private static DBHelper instance;

/**
* 构造方法
* @param context
*/
public DBHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
/**
* 获取单例
* @param context
* @return
*/
public static synchronized DBHelper getHelper(Context context) {
context = context.getApplicationContext();
if (instance == null) {
synchronized (DBHelper.class) {
if (instance == null) {
instance = new DBHelper(context);
}
}
}
return instance;
}

/**
* 这里创建表
*/
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
L.e(OrmliteActivity.TAG, "执行onCreate,VERSION="+VERSION);
// 创建表
try {
/**
* 逻辑混乱下面的表
*/
TableUtils.createTable(connectionSource, PackageInfo.class);
TableUtils.createTable(connectionSource, Photographer.class);
TableUtils.createTable(connectionSource, Theme.class);
TableUtils.createTable(connectionSource, Img.class);
/**
* 以下面的为主
*/
TableUtils.createTable(connectionSource, User.class);
TableUtils.createTable(connectionSource, Article.class);

} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* 这里进行更新表操作
*/
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
L.e(OrmliteActivity.TAG, "执行onUpgrade,VERSION="+VERSION);
try
{
//            /**
//             * 逻辑混乱下面的表
//             */
//            TableUtils.dropTable(connectionSource, PackageInfo.class, true);//这种是直接删掉表了
//            TableUtils.dropTable(connectionSource, Photographer.class, true);
//            TableUtils.dropTable(connectionSource, Theme.class, true);
//            TableUtils.dropTable(connectionSource, Img.class, true);
//            /**
//             * 以下面的为主
//             */
//            TableUtils.dropTable(connectionSource, User.class, true);
//            TableUtils.dropTable(connectionSource, Article.class, true);
//            onCreate(sqLiteDatabase, connectionSource);

String sql1 = "";
for(int i=oldVersion;i<newVersion;i++) {
switch (i) {
case 1://数据库版本1 升级到 版本2
//对table 增加字段
sql1 = "alter table tb_user add age integer";
getDao(User.class).executeRawNoArgs(sql1);
break;
case 2://数据库版本2 升级到 版本3
sql1 = "alter table tb_user add height integer";
getDao(User.class).executeRawNoArgs(sql1);
break;
}
}
} catch (SQLException e)
{
e.printStackTrace();
}
}

/**
* 通过类来获得指定的Dao
*/
public synchronized Dao getDao(Class clazz) throws SQLException {
Dao dao = null;
String className = clazz.getSimpleName();
if (daos.containsKey(className)) {
dao = daos.get(className);
}else{
dao = super.getDao(clazz);
daos.put(className, dao);
}
return dao;
}

/**
* 释放资源
*/
@Override
public void close() {
super.close();
for (String key : daos.keySet()) {
Dao dao = daos.get(key);
dao = null;
}
}
}


demo MyFrame../testormlite

User表和Article表 user表测试升级版本,下面是测试结果

版本1 升级到 2 再升级到 3

※version=1 run一次 add;version=2 run一次 add;version=3 run一次 add;

※注意要对应操作user.java类的字段增加 下面的亦如此

logcat:

执行onCreate,START_VERSION=1

add success result=1

执行onUpgrade,START_VERSION=2

add success result=1

执行onUpgrade,START_VERSION=3

add success result=1



版本1 升级到 3

※version=1 run一次 add;version=3 run一次 add

执行onCreate,START_VERSION=1

add success result=1

执行onUpgrade,START_VERSION=3

add success result=1



用户第一安装就是版本3

※version=1 run一次 add

执行onCreate,START_VERSION=3

add success result=1



版本不升级,删除字段(接3继续操作)

删除height,可以添加,不会报错,但height无值

执行onCreate,START_VERSION=3

add success result=1

add success result=1



版本不升级,增加字段,这个是会报错的呢

接4操作,回复height字段,并追加一个weight字段(注意此时版本还是没变,依然是3)



执行onCreate,START_VERSION=3

add success result=1

add success result=1

add fail result=0,Message=Unable to run insert stmt on object User [id=102, name=weight]: INSERT INTO
tb_user
(
id
,
name
,
age
,
height
,
weight
) VALUES (?,?,?,?,?)

数据库没有插入成功,通过root权限后的工具查看,user表没有发生变化

git项目下载地址

https://github.com/oceanhai/MyFrame.git

项目里比较杂,还涉及到ndk什么的,自己工具除了需要sdk,还需要对应ndk工具等
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐