Ormlite的工具使用

配置

compile 'com.j256.ormlite:ormlite-android:5.0'

使用

常用注解

@DatabaseTable(tableName = "t_user")

指定实体和表的一一对应关系

@DatabaseField()

指定属性与表中列的一一对应关系

常用配置说明:

主键:id = true

自增主键:generatedId = true

列名:columnName = "name"

是否为空:canBeNull = true

外键:foreign = true

示例:

@DatabaseTable(tableName ="t_address")

public class AddressBean {

@DatabaseField(id =true)

private int_id;

@DatabaseField(canBeNull =false)

privateStringgoodsAddress;

@DatabaseField(canBeNull =false)

privateStringvillage;

@DatabaseField(canBeNull =false,foreign =true,columnName ="user_id",foreignColumnName ="_id")

privateUserBeanuser;

}

@DatabaseTable(tableName ="t_user")

public classUserBean {

@DatabaseField(id =true)

private int_id;

@ForeignCollectionField(eager =true)

private ForeignCollectionaddressList;

}

DBHelper管理

与原声的DBHelper相同,主要负责数据库的创建和表的维护。需要继承OrmLiteSqliteOpenHelper类。

使用工具:TableUtils完成表的相关操作,如创建、清空、删除

public classDBHelper extends OrmLiteSqliteOpenHelper{

private static final String DATABASENAME="yijia.db";

private static final int DATABASEVERSION=1;

public DBHelper(Context context) {

super(context,DATABASENAME, null,DATABASEVERSION);

}

@Override

public void onCreate(SQLiteDatabase database,ConnectionSource connectionSource) {

try{

TableUtils.createTable(connectionSource,UserBean.class);

TableUtils.createTable(connectionSource,AddressBean.class);

}catch(SQLException e) {

e.printStackTrace();

}

}

@Override

public voidonUpgrade(SQLiteDatabase database,ConnectionSource connectionSource, intoldVersion, intnewVersion) {

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值