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。
- Android ORMLite实现持久化
- Android中使用ormlite实现持久化--HelloOrmLite
- Android中使用ormlite实现持久化--…
- Android中使用ormlite实现持久化--HelloOrmLite
- Android中使用ormlite实现持久化--HelloOrmLite
- Android中使用ormlite实现持久化--HelloOrmLite
- Android中使用ormlite实现持久化(一)--HelloOrmLite
- Android中使用ormlite实现持久化(一)--HelloOrmLite
- Android中使用Ormlite实现持久化(二)--持久化类的详细配置
- Android中如何使用OrmLite持久化
- android数据库持久化框架, ormlite框架,
- Android使用Ormlite实现Java Bean的持久化:Dao的封装与使用
- Android使用Ormlite实现Java Bean的持久化:Dao的封装与使用
- Android中使用OrmLite持久化的详细介绍
- Android中使用OrmLite持久化的详细介绍
- ormLite框架持久化编程
- Android 使用ORMLite打造万能泛型Dao简化数据持久化层
- ormlite安卓数据持久化框架的使用
- JWASM x64语法
- matlab滤波器设计
- tomcat启动shell编写
- 网络编程:Reactor与Proactor的概念
- 一个简单的NoSQL内存数据库—Berkeley DB基本操作的例子
- Android ORMLite实现持久化
- [数字图像处理]灰度变换——反转,对数变换,伽马变换,灰度拉伸,灰度切割,位图切割
- CUDA: Threading的Block和Grid的設定與 Warp
- Android sd卡使用解说
- 关于GCD的8题
- SQLServer的数据类型
- 二进制和XML两种网络数据格式的比较
- .NET编码常用转换
- oracle 查询出一段时间内连续时间间隔的记录集