120 lines
3.3 KiB
Plaintext
Executable File
120 lines
3.3 KiB
Plaintext
Executable File
package com.cm.utils.db;
|
||
|
||
import android.content.Context;
|
||
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 java.util.Properties;
|
||
|
||
/**
|
||
* Created by Xuer on 2017/3/9.
|
||
*/
|
||
public class DBHelper extends OrmLiteSqliteOpenHelper {
|
||
Context context;
|
||
private static DBHelper instance;
|
||
private Map<String, Dao> daos = new HashMap<String, Dao>();
|
||
|
||
/**
|
||
* @param context
|
||
* @param dbName 数据库名称
|
||
*/
|
||
private DBHelper(Context context, String dbName) {
|
||
super(context, dbName, null, 1);
|
||
this.context = context;
|
||
}
|
||
|
||
|
||
@Override
|
||
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
|
||
|
||
try {
|
||
for (String table : getTableList())
|
||
TableUtils.createTable(connectionSource, Class.forName(table));
|
||
} catch (SQLException e) {
|
||
e.printStackTrace();
|
||
} catch (ClassNotFoundException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
|
||
|
||
}
|
||
|
||
/**
|
||
* 单例获取该Helper
|
||
*
|
||
* @param dbName 数据库名称
|
||
* @param context
|
||
* @return
|
||
*/
|
||
public static synchronized DBHelper getHelper(Context context, String dbName) {
|
||
context = context.getApplicationContext();
|
||
if (instance == null) {
|
||
synchronized (DBHelper.class) {
|
||
if (instance == null)
|
||
instance = new DBHelper(context, dbName);
|
||
}
|
||
}
|
||
|
||
return instance;
|
||
}
|
||
|
||
/**
|
||
* getDao为一个泛型方法,
|
||
* 会根据传入Class对象进行创建Dao,
|
||
* 并且使用一个Map来保持所有的Dao对象,
|
||
* 只有第一次调用时才会去调用底层的getDao()。
|
||
* @param clazz
|
||
* @return
|
||
* @throws SQLException
|
||
*/
|
||
public synchronized Dao getDao(Class clazz) throws SQLException {
|
||
Dao dao = null;
|
||
String className = clazz.getSimpleName();
|
||
|
||
if (daos.containsKey(className)) {
|
||
dao = daos.get(className);
|
||
}
|
||
if (dao == null) {
|
||
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;
|
||
}
|
||
}
|
||
//读取配置文件,获取所需要建的表
|
||
public String[] getTableList() {
|
||
String[] tables = null;
|
||
Properties properties = new Properties();
|
||
try {
|
||
properties.load(context.getAssets().open("dbtable.prop"));
|
||
String tablelist = properties.getProperty("tablelist");
|
||
tables = tablelist.split(",");
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
return tables;
|
||
}
|
||
}
|