要创建一个按名称和密码查找帐户的查询,您可以执行以下操作:
QueryBuilder<Account, String> qb = accountDao.queryBuilder();
Where where = qb.where();
// the name field must be equal to "foo"
where.eq(Account.NAME_FIELD_NAME, "foo");
// and
where.and();
// the password field must be equal to "_secret"
where.eq(Account.PASSWORD_FIELD_NAME, "_secret");
PreparedQuery<Account, String> preparedQuery = qb.prepareQuery();
这是我在项目中使用 ORMLite 从 SQLite 获取 PropertyModel 的代码
public ArrayList<PropertyPicModel> selectArgumentQueryPropertyModel(int property_id, Dao<PropertyPicModel, Integer> dao)
{
try {
QueryBuilder<PropertyPicModel, Integer> queryBuilder = dao.queryBuilder();
Where<PropertyPicModel, Integer> where = queryBuilder.where();
SelectArg selectArg = new SelectArg();
// define our query as 'property_id = ?'
where.eq(ORMLiteConfig.PROPERTY_ID, selectArg);
// prepare it so it is ready for later query or iterator calls
PreparedQuery<PropertyPicModel> preparedQuery = queryBuilder.prepare();
selectArg.setValue(property_id);
// later we can set the select argument and issue the query
ArrayList<PropertyPicModel> picList = (ArrayList<PropertyPicModel>) dao.query(preparedQuery);
return picList;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
MyLog.e("Excep selectArgumentQuery " +e.toString());
}
catch (Exception e) {
MyLog.e("Excep selectArgumentQuery " +e.toString());
}
return null;
}
在此示例中,将生成的 SQL 查询大致为:
SELECT * FROM account WHERE (name = 'foo' AND passwd = '_secret')
https://github.com/AshishPsaini/ormlite-examples/tree/master/android/HelloAndroid
http://ormlite.com/docs/in