Android ORMLite实现持久化

来源:互联网 发布:创业软件股票解禁 编辑:程序博客网 时间:2024/05/16 19:20

  今天介绍一款第三方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 = "";@DatabaseFieldprivate 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);}@Overridepublic 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());}}@Overridepublic 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 onesonCreate(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;}@Overridepublic 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;@Overrideprotected 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;@Overrideprotected 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();}}@Overrideprotected 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;}}

  更多的详细内容:http://ormlite.com/sqlite_java_android_orm.shtml。