数据库框架为OrmLite
主要代码
public class SQLiteHelper extends OrmLiteSqliteOpenHelper {
private static String DB_NAME;
public static final int DB_VERSOIN = 21;
private static SQLiteHelper instance;
private List<Class<Object>> DBTables = new ArrayList<Class<Object>>();
public static SQLiteHelper getInstance(Context context){
if(instance == null){
synchronized (SQLiteHelper.class){
if(instance == null){
DB_NAME = MyApplication.getInstance().mMydb + "/gnhkg.db";
instance = new SQLiteHelper(context);
}
}
}
return instance;
}
public SQLiteHelper(Context context) {
super(context, DB_NAME, null, DB_VERSOIN);
}
/**
* 创建表的操作
* @param database
* @param connectionSource
*/
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
DBTables.clear();
updateTables(DBTables);
for (Class table : DBTables) {
TableUtils.createTable(connectionSource,table);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 这里进行更新表操作
* @param database
* @param connectionSource
* @param oldVersion
* @param newVersion
*/
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
if(newVersion > oldVersion){
try {
TransactionManager.callInTransaction(connectionSource, new Callable<Boolean>() {
@Override