Greendao:插入,更新,删除语法,最佳实践

时间:2013-02-17 08:40:06

标签: android greendao

我在这里问了类似的问题,但我没有得到任何满意的答复,所以请理解我的担忧:

    1. https://stackoverflow.com/questions/14846105/insert-ot-update-using-one-of-the-field-as-key-in-greendao
    2. https://stackoverflow.com/questions/14547288/best-way-to-select-row-with-following-scenario-using-greendao

我在服务器和客户端都有一个名为“TARGET”的表 这里Server是MySql,Client是Android。

我想将 greendao 用于客户端部分, 我有多个任务,如下:

  1. 拥有 EMPLOYEE_ID 的员工将在特定日期之后从服务器获取所有目标,现在 在客户端部分,如果 TARGET 存在,则应更新,否则插入。 这是 insertOrUpdate 案例。
  2. 使用 TARGET_ID
  3. 删除某个目标
  4. 使用 TARGET_NAME 获取TARGET列表。
  5. 有关问题的数据:

        TABLE : TARGET
        FIELDS: TARGET_ID
                TARGET_NAME
                EMPLOYEE_ID
    

    在DaoExampleGenerator中使用以下代码:
    Best way to select row with following scenario using greenDao?

        private static void addTarget(Schema schema){
            Entity target = schema.addEntity("TARGET");
            target.addStringProperty("TARGET_ID").primaryKey().autoincrement();
            target.addStringProperty("TARGET_NAME");
            target.addStringProperty("EMPLOYEE_ID");
        }
    

    以下是我为发布的问题所做的工作:

        1. tDao.insertOrReplaceInTx(tArrayList);
           where, tArrayList is ArrayList of TARGET Object.
        2. for deleting a target using TARGET_ID, i load all the targets in     
           ArrayList<TARGET> then check TARGET_ID of each TARGET object. If 
           the TARGET_ID matches then i use, **tDao.delete(t);**
        3. for this also i do the same as (2), i load all the targets then match 
           the TARGET_NAME, If it matches then i add it to list.
    

    任何人都可以告诉我实施上述问题陈述的最佳方法。 使用 greendao

2 个答案:

答案 0 :(得分:1)

您应该能够构建删除查询。正如GreenDao docs所说:

  

删除查询

     

批量删除不会删除单个实体,而是删除所有实体   符合一些标准。要执行批量删除,请创建一个   QueryBuilder,调用其buildDelete方法,并执行返回的   DeleteQuery。这部分API将来可能会发生变化,例如:   可以添加便利方法等。请记住,批量删除   目前不影响身份范围内的实体,例如你可以   “复活”已删除的实体,如果它们之前已被缓存并且是   通过他们的ID访问(加载方法)。考虑清除身份   现在的范围,如果这可能会导致您的用例出现问题。

答案 1 :(得分:0)

看来你现在所做的只是你问题的唯一答案...... 据我所知,greenDao中没有按标准获取行的方法或按标准删除单行...只有ID(长)...

相关问题