仅查询一个字符串

时间:2014-06-03 07:23:19

标签: java android android-sqlite ormlite

我最近从原始SQLite切换到ORMLite现在我有一个名为user的对象表,其中max rows总是1(总是只有1个用户),我怎么能用ORMLite查询这个表中的一个字符串或整数,如user id或hash或simmilar?

以旧方式我这样做:

public int getCode() {
    int result = -1;
    if (db != null) {
        String rawQuery = "SELECT code FROM settings;";
        Cursor crs = db.rawQuery(rawQuery, null);
        if (crs != null && crs.getCount() > 0 && crs.moveToNext()) {
            result = crs.getInt(crs.getColumnIndex("code"));
        }
    }
    return result;
}

那么ORMLite应该怎么样?

1 个答案:

答案 0 :(得分:1)

您可以参考从ORMLite documentation获取的此示例:

// get our query builder from the DAO
QueryBuilder<Account, String> queryBuilder = accountDao.queryBuilder();
// the 'password' field must be equal to "qwerty"
queryBuilder.where().eq(Account.PASSWORD_FIELD_NAME, "qwerty");
// query for all accounts that have "qwerty" as a password
List<Account> accountList = queryBuilder.query();