Android数据库管理-ActiveAndroid

ActiveAndroid是一个轻量级的ORM框架,可以以类的方式简单快捷地进行数据库的管理,而无需编写一个单独的SQL语句。
ActiveAndroid git地址

配置

1、AndroidManifeset中添加如下配置:

        <application
        ...
        android:name="com.activeandroid.app.Application"
        >

        <meta-data
            android:name="AA_DB_NAME"
            android:value="xxx.db" />
        <meta-data
            android:name="AA_DB_VERSION"
            android:value="7" />

        <meta-data
            android:name="AA_MODELS"
            android:value="com.syd.oden.odendemo.entity.sqltab.LocationTab, com.syd.oden.odendemo.entity.sqltab.MusicFavorTab" />

AA_MODELS为数据库中表的实体类

2、Application继承com.activeandroid.app.Application

public class MyApplication extends com.activeandroid.app.Application {}

或者

public class MyApplication extends SomeLibraryApplication {
    @Override
    public void onCreate() {
        super.onCreate();
        ActiveAndroid.initialize(this);
    }
        @Override
    public void onTerminate() {
        super.onTerminate();
        ActiveAndroid.dispose();
    }
}

3、创建表

@Table(name = "PictureTabs")
public class PictureTab extends Model {
    private static MyLog myLog = new MyLog("[PictureTab] ");

    @Column(name = "dirName")
    String dirName;

    @Column(name = "fileName")
    String fileName;

    @Column(name = "describe")
    String describe;

    @Column(name = "longitude")
    double longitude;

    @Column(name = "latitude")
    double latitude;

    public PictureTab() {
        super();
    }

    public PictureTab(String dirName, String fileName, double longitude, double latitude) {
        super();
        this.dirName = dirName;
        this.fileName = fileName;
        this.longitude = longitude;
        this.latitude = latitude;
    }
}

增删改查

       for (int i=0; i<5; i++) {
            DbBlesGroup dbBleGroup = new DbBlesGroup();
            dbBleGroup.groupIndex = i;
            dbBleGroup.groupName = "groupName" + i;
            dbBleGroup.addr = "addr" + i;
            dbBleGroup.name = "name" + i;
            dbBleGroup.save();
        }

查出所有

List<DbBlesGroup> dbBleGroupList = new ArrayList<>();
        dbBleGroupList = new Select()
                .from(DbBlesGroup.class)
                .orderBy("groupName ASC")
                .execute();

        for (int i=0; i<dbBleGroupList.size(); i++)
        {
            L.d("dbBleGroupList :" + dbBleGroupList.get(i).groupName);
        }

指定条件查找

List<DbBlesGroup> dbBleGroupList = new ArrayList<>();
        dbBleGroupList = new Select()
                .from(DbBlesGroup.class)
                .where("groupName = ?", "groupName3")
                .orderBy("groupName ASC")
                .execute();

多条件查找

newSelect().from(UserViewTab.class).where("viewId=? and bleAddr=?",viewId,addr).executeSingle();

使用事务(transaction)

ActiveAndroid.beginTransaction();
try {
        for (int i = 0; i < 100; i++) {
            Item item = new Item();
            item.name = "Example " + i;
            item.save();
        }
        ActiveAndroid.setTransactionSuccessful();
}
finally {
        ActiveAndroid.endTransaction();
}

.orderBy(“id DESC”)降序
.orderBy(“id ASC”)升序

删除

new Delete().from(DbBlesGroup.class).where("groupName = ?", "groupName2").execute();

new Update(DbBlesGroup.class).set("addr = ?", "123").where("groupName = ?", "groupName2").execute();

也可直接用save修改

注意事项

1、构造方法中记得加入super();

2、在sudio2.2运行报错解决:

erro: 'java.lang.String com.activeandroid.TableInfo.getTableName()' on a null object reference.

关掉Instant Run

3、表中包含另一个表,则保存的时候要先保存另一个表;
发现一个bug,表中包含另一个表,查另一个表里的数据可能有误

 recipeAlarmList.add(RecipeAlarmTab.getById(recipeTab.getRecipeAlarmTab1().getId())); //activeAndroid貌似有bug,故通过ID重新查询一次
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值