现在的位置: 首页 > 综合 > 正文

Android ormlite 框架介绍

2014年01月28日 ⁄ 综合 ⁄ 共 2650字 ⁄ 字号 评论关闭
 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 findData(Dao Entitydao, int id) throws SQLException{

  HashMap EntityMap = new HashMap();

  userguideMap.put("id", id);

  List EntityLists = entityDao.queryForFieldValues(EntityMap);

  return EntityLists == null ? null : EntityLists;

  }

  public void addEntity(Dao 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 dataDao = getHelper().getDataDao();

  //查找操作调用

  List simpledataList = dao.findData(dataDao, 1);

  //添加操作调用

  dao.addEntity(dataDao,"demotest");

  } catch (SQLException e) {

  e.printStackTrace();

抱歉!评论已关闭.