Android 查询id需要整数参数

Android 查询id需要整数参数,android,eclipse,orm,ormlite,Android,Eclipse,Orm,Ormlite,文件中提到了以下内容: Account account = accountDao.queryForId("John Smith"); if (account == null) { // the name "John Smith" does not match any rows } 但是在Eclipse(android)中,我只看到将整数作为参数传递的选项? 有什么帮助吗?首先,您应该定义字符串类型的实体ID: @DatabaseTable() public class Accou

文件中提到了以下内容:

Account account = accountDao.queryForId("John Smith");
if (account == null) {
    // the name "John Smith" does not match any rows
}   
但是在Eclipse(android)中,我只看到将整数作为参数传递的选项?
有什么帮助吗?

首先,您应该定义字符串类型的实体ID:

@DatabaseTable()
public class Account {

    @DatabaseField(id = true)
    private String mFullName;
    ...
}
然后您应该根据实体类型及其ID类型声明Dao对象。现在,您可以使用ID类型为字符串的queryForId:

Dao<Account, String> accountDao = getAccountDao();
Account account = accountDao.queryForId("John Smith");
if (account == null) {
    // the name "John Smith" does not match any rows
}
Dao accountDao=getAccountDao();
Account=accountDao.queryForId(“约翰·史密斯”);
如果(帐户==null){
//名称“John Smith”与任何行都不匹配
}

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

Dao<Account, Integer> accountDao = getDao(Account.class);
然后您可以通过
字符串
id查询
帐户

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