android数据库持久化框架, ormlite框架,

时间:2021-12-10 13:21:43

前言

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();

}

http://my.oschina.net/HandyWorkGroup/blog/77473