zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    Show me the Template
    WPF中的Style(风格,样式)
    像苹果工具条一样平滑连续地缩放
    为窗体添加 "最大化","最小化","还原"等 事件
    [CHM]果壳中的XAML(XAML in a Nutshell)
    我的简约播放器
    很好玩的滚动效果
    项目经验分享(上)
    通过mongodb客户端samus代码研究解决问题
    记录数据库执行情况来分析数据库查询性能问题
  • 原文地址:https://www.cnblogs.com/CharlesGrant/p/5520840.html
Copyright © 2011-2022 走看看