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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值