今天介绍一款第三方ORMLite库的简单用法。
1、下载ORMLite Jar(ormlite-core.jar、 ormlite-android.jar):http://ormlite.com/releases/。并把库添加到libs文件夹下。
2、配置一个实体类:
@DatabaseTable(tableName = "tb_cache")
public class CacheVO {
@DatabaseField(unique = true, index = true, id = true)
private String id = "";
@DatabaseField
private String value = "";
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
@DatabaseTable声明一个名为tb_cache的表,@DatabaseField声明表中的一个变量。
3、创建一个database helper类继承OrmLiteSqliteOpenHelper,实现onCreate(SQLiteDatabase db, ConnectionSource connectionSource)、onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersoin, int newVersion)方法:
public class DBHelper extends OrmLiteSqliteOpenHelper {
private static final String TAG = DBHelper.class.getSimpleName();
private static final String DATABASE_NAME = "aimanalytic.db";
private static final int DATABASE_VERSION = 1;
private Dao<CacheVO, String> cacheDao = null;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try {
LogHelper.i(TAG, "onCreate db");
TableUtils.createTable(connectionSource, CacheVO.class);
} catch (Exception ex) {
LogHelper.e(TAG, ex.getMessage());
}
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
int oldVersoin, int newVersion) {
try {
LogHelper.i(TAG, "onUpgrade");
TableUtils.dropTable(connectionSource, CacheVO.class, true);
// after we drop the old databases, we create the new ones
onCreate(db, connectionSource);
} catch (Exception e) {
LogHelper.e(TAG, "Can't drop databases" + e.getMessage());
throw new RuntimeException(e);
}
}
public Dao<CacheVO, String> getCacheDao() throws SQLException {
if (cacheDao == null) {
cacheDao = getDao(CacheVO.class);
}
return cacheDao;
}
@Override
public void close() {
super.close();
cacheDao = null;
}
}
4、在activity中应用,一种是继承OrmLiteBaseActivity:
public class MainActivity extends OrmLiteBaseActivity<DBHelper> {
protected static final String TAG = MainActivity.class.getSimpleName();
private static AccountVO accountVO = null;
public static DBHelper DB_HELPER = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化数据库
DB_HELPER = getHelper();
try {
Dao<CacheVO, String> cacheDao = MainApp.DB_HELPER.getCacheDao();
CacheVO cVo = cacheDao.queryForId(AccountVO.class.getSimpleName());
if (cVo != null) {
JSONObject dataObj = new JSONObject(cVo.getValue());
accountVO = AccountVO.getParseJSON(dataObj);
Log.i(TAG, accountVO.getToken());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
另一种不继承OrmLiteBaseActivity:
public class MainActivity extends Activity {
protected static final String TAG = MainActivity.class.getSimpleName();
private static AccountVO accountVO = null;
public static DBHelper DB_HELPER = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化数据库
DB_HELPER = getHelper();
try {
Dao<CacheVO, String> cacheDao = MainApp.DB_HELPER.getCacheDao();
CacheVO cVo = cacheDao.queryForId(AccountVO.class.getSimpleName());
if (cVo != null) {
JSONObject dataObj = new JSONObject(cVo.getValue());
accountVO = AccountVO.getParseJSON(dataObj);
Log.i(TAG, accountVO.getToken());
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (MainApp.DB_HELPER != null) {
OpenHelperManager.releaseHelper();
MainApp.DB_HELPER = null;
}
}
private DBHelper getHelper() {
if (DB_HELPER == null) {
DB_HELPER = OpenHelperManager.getHelper(this, DBHelper.class);
}
return DB_HELPER;
}
}