Android ORMLite实现持久化

  今天介绍一款第三方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;
	}

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值