Eli
Eli

Reputation: 85

Query for id needs integer parameter

In the documentation the following is mentioned:

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

But in Eclipse(android) i only see the option to pass an integer as parameter? any help?

Upvotes: 3

Views: 1935

Answers (2)

Gray
Gray

Reputation: 116888

The Dao objects use generics to enforce the type of id is associated with your entity. If you only see the option to pass an integer into dao.queryForId(...) then you probably, mistakenly, defined the dao like:

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

The first generic parameter specifies the type of the entity and the second generic parameter specifies the type of the ID field in that entity. With the Integer, you will call accountDao.queryForId(Integer).

As @Tomas mentioned, you need to define your DOA with something like:

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

Then you can query for Account by a String id:

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

Upvotes: 10

Tomas Žemaitis
Tomas Žemaitis

Reputation: 1907

First you should define what entities ID is of String type:

@DatabaseTable()
public class Account {

    @DatabaseField(id = true)
    private String mFullName;
    ...
}

Then you should declare Dao object according to entity type ant its ID type. Now you can use queryForId with ID type of String:

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

Upvotes: 3

Related Questions