• dbflow 批量 增删查改


    @ModelContainer
    @Table(database = DemoDatabase.class)
    class Person extends BaseModel implements Serializable {
        @PrimaryKey()
        int uid;
        @Column
        int age;
        @Column
        String name;
    
        ....
        ....  
    }

    下面例子主要用以上实体类。

    1、可以通过增加外键来关联查询

    // 比如增加infoId作为外键
    
    // 1 查询
    // 使用Table类进行字段查询
    List<Person> persons = new Select().from(Person.class)
                    .where(Person_Table.infoId.eq(infoId))
                    .queryList();
    
    // 2 删除
    // 可通过写Condition条件进行删除。
    SQLCondition condition = Condition.column(Person_Table.infoId.getNameAlias()).eq(infoId);
    
    Delete.table(Person.class).where(condition);

    2、通过in进行操作。

    List<Integer> uidList = new ArrayList<>();
    SQLCondition condition = Condition.column(Person_Table.uid.getNameAlias()).in(uidList);
    
    // 1 查询
    List<Person> persons = new Select().from(Person.class)
                    .where(condition)
                    .queryList();
    
    // 2 删除
    Delete.table(Person.class, condition);

    dbflow保存操作:

    在github 的 issue上有一个bug,上面说,db.reset();后,保存会出现主键是唯一的异常。我更新了beta6后,发现不能使用一个批量保存list的方法了。

    即是使用:

    new SaveModelTransaction<>(ProcessModelInfo.withModels(peoples)).onExecute();

    在save方法上出现如下异常:

    android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique
    ....
    ....
    ....

    这个相关联的类以及方法,全部在beta6版本中去除。然后只提供了事务管理。

    让我们自己去实现事务批量保存,结果可以了。完美兼容。

    DatabaseDefinition database = FlowManager.getDatabase(DemoDatabase.class);
            Transaction transaction = database.beginTransactionAsync(new ITransaction() {
                @Override
                public void execute(DatabaseWrapper databaseWrapper) {
              // todo 处理list保存
              ...
              ...
           }
            }).build();
    transaction.execute();
  • 相关阅读:
    [转][Linux/Ubuntu] vi/vim 使用方法讲解
    [转]在Windows中安装Memcached
    memcached可视化客户端工具
    [转]C#操作Memcached帮助类
    [转]Redis和Memcache区别,优缺点对比
    [转]【转】大型高性能ASP.NET系统架构设计
    [转]浅谈命令查询职责分离(CQRS)模式
    element-UI——el-table添加序号
    xss攻击(转)
    vuex原理
  • 原文地址:https://www.cnblogs.com/CharlesGrant/p/5520840.html
Copyright © 2020-2023  润新知