【问题标题】:Query for id needs integer parameter查询id需要整数参数
【发布时间】:2013-03-21 08:58:54
【问题描述】:

在文档中提到了以下内容:

Account account = accountDao.queryForId("John Smith");
if (account == null) {
    // the name "John Smith" does not match any rows
}   

但在 Eclipse(android) 中,我只看到将整数作为参数传递的选项? 有什么帮助吗?

【问题讨论】:

    标签: android eclipse orm ormlite


    【解决方案1】:

    Dao 对象使用泛型来强制 id 的类型与您的实体相关联。如果您只看到将整数传递给 dao.queryForId(...) 的选项,那么您可能错误地将 dao 定义为:

    Dao<Account, Integer> accountDao = getDao(Account.class);
    

    第一个泛型参数指定实体的类型,第二个泛型参数指定该实体中 ID 字段的类型。使用Integer,您将调用accountDao.queryForId(Integer)

    正如@Tomas 所提到的,您需要使用以下内容定义您的 DOA:

    Dao<Account, String> accountDao = getDao(Account.class);
    

    然后您可以通过String id 查询Account

    Account account = accountDao.queryForId("John Smith");
    

    【讨论】:

      【解决方案2】:

      首先你应该定义什么实体ID是String类型的:

      @DatabaseTable()
      public class Account {
      
          @DatabaseField(id = true)
          private String mFullName;
          ...
      }
      

      那么你应该根据实体类型和它的ID类型来声明Dao对象。现在您可以使用 ID 类型为 String 的 queryForId:

      Dao<Account, String> accountDao = getAccountDao();
      Account account = accountDao.queryForId("John Smith");
      if (account == null) {
          // the name "John Smith" does not match any rows
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-11
        • 1970-01-01
        • 1970-01-01
        • 2017-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多