android數據庫持久化框架
前言
Android中內置了SQLite,但是對于數據庫操作這塊,非常的麻煩.其實可以試用第3方的數據庫持久化框架對之進行結構上調整, 擺脫了訪問數據庫操作的細節,不用再去寫復雜的SQL語句.雖然這樣會在數據庫操作上損失一點性能,但基于xxxx對數據庫操作不頻繁性能要求不高可以使用,所帶來的好處即使有一個良好的統一的數據庫操作以及降低代碼維護成本.
適合與android的數據庫持久化框架主流的有2種: androrm和ormlite.簡單的對這2種框架以及SQLite在CPU:1GHz,RAM:512M的android的及其上進行插入1W條數據的性能測試,得到的結果如下: SQLite=287.488s; androrm=310.562s;ormlite=333.760s.可見使用原始的SQLite性能最高. 由于ormlite用注解字段的方式,使得在性能有著一定的損失. 不過, ormlite架構更適合java,而且類似hibernate,而androrm適合與python.
下面就對ormlite框架進行簡單的介紹.
1. ormlite框架
1. 從http://ormlite.com/releases/下載對應的核心包core及android支持庫.然后在項目中加入兩個jar包.
2. 存儲的數據對象實體
public class Entity{
@DatabaseField(generatedId = true)//自增長的主鍵
int id;
@DatabaseField//聲明string為數據庫字段
String string;
public Entity() {
//ormlite中必須要有一個無參的構造函數
}
...
}
ormlite是通過注解方式配置該類的持久化參數,其中常用參數如下:
1.表名:不指定的話表名就是類名.
@DatabaseTable(tableName="dataTableName")
2.字段:這個可以配置的屬性有點多.
@DatabaseField
(1)主鍵:
@DatabaseField(id=true)
(2)列名: 不指定的話就是和變量名一樣的
@DatabaseField(columnName="columnName")
(3) 數據類型: 這個一般情況下都不用指定,可以根據java 類獲得
@DatabaseField(dataType=DataType.INTEGER)
(4) 默認值:
@DatabaseField(defaultValue="0")
(5)長度:一般用于String型
@DatabaseField(width=13)
(6) 能否為空:默認為True
@DatabaseField(canBeNull=false)
3. 需要數據DataHelper類,來創建及管理數據庫. DataHelper類繼承OrmLiteSqliteOpenHelper.并在覆蓋實現onCreate, onUpgrade, close等方法.
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource){
try{
Log.e(DataHelper.class.getName(), "開始創建數據庫");
TableUtils.createTable(connectionSource, Entity.class);
Log.e(DataHelper.class.getName(), "創建數據庫成功");
}catch(SQLException e){
Log.e(DataHelper.class.getName(), "創建數據庫失敗", e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int arg2, int arg3){
try{
TableUtils.dropTable(connectionSource, Entity.class, true);
onCreate(db, connectionSource);
Log.e(DataHelper.class.getName(), "更新數據庫成功");
}catch(SQLException e){
Log.e(DataHelper.class.getName(), "更新數據庫失敗", e);
}
}
@Override
public void close(){
super.close();
}
4. 需要相應的數據Dao類,即數據訪問接口.
public class EntityDao{
public List<Entity> findData(Dao<Entity, Integer> Entitydao, int id) throws SQLException{
HashMap<String, Object> EntityMap = new HashMap<String, Object>();
userguideMap.put("id", id);
List<Entity> EntityLists = entityDao.queryForFieldValues(EntityMap);
return EntityLists == null ? null : EntityLists;
}
public void addEntity(Dao<Entity, Integer> entityDao, String string) throws SQLException{
Entity Entity =new Entity(string);
entityDao.create(Entity);
}
}
5. 調用
ormlite有兩種使用方法,一種是繼承對應的OrmLiteBaseActivity.但像xxxx Activity本身必須繼承BaseActivity,這樣就必須使用另外一種方法:在activity中添加一個獲取helper的方法,還有在onDestroy中添加及時關閉helper的方法。
private DataHelper dataHelper = null;
private DataHelper getHelper() {
if (dataHelper == null) {
dataHelper = OpenHelperManager.getHelper(this, DataHelper.class);
}
return dataHelper;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (dataHelper != null) {
OpenHelperManager.releaseHelper();
dataHelper = null;
}
}
其中數據庫操作實現代碼如下:
EntityDao dao = new EntityDao();
try {
Dao<Entity, Integer> dataDao = getHelper().getDataDao();
//查找操作調用
List<Entity> simpledataList = dao.findData(dataDao, 1);
//添加操作調用
dao.addEntity(dataDao,"demotest");
} catch (SQLException e) {
e.printStackTrace();
}