1

我有一些字段的 ORMLite 数据库。我想从我从 web 服务获得的 id == id 的表中选择标题。我喜欢这样:

 try {
    Dao<ProcessStatus,Integer> dao = db.getStatusDao(); 
    Log.i("status",dao.queryForAll().toString());
    QueryBuilder<ProcessStatus,Integer> query = dao.queryBuilder();
    Where where = query.where();
    String a = null;
    for(Order r:LoginActivity.orders) {
        //LoginActivity.orders - array of my objects which I get from webservice
        Log.i("database",query.selectRaw("select title from process_status").
            where().rawComparison(ProcessStatus.STATUS_ID, "=",
                       r.getProcess_status().getProccessStatusId()).toString());
    }
    Log.i("sr",a);
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

我试过这样,但我只得到我的身份证,而不是标题。我试过这样:

Log.i("database", query.selectColumns(ProcessStatus.STATUS_TITLE).where().
    eq(ProcessStatus.STATUS_ID, r.getProcess_status().getProccessStatusId())
    .toString());

但我有同样的结果。我应该如何从数据库中获取数据?

4

2 回答 2

14

要从表中选择特定字段,您可以执行以下操作:

String result = "";
try {
    GenericRawResults<String[]> rawResults = yourDAO.queryRaw("select " +
        ProcessStatus.STATUS_TITLE +" from YourTable where "+ 
        ProcessStatus.STATUS_ID + " = " + 
        r.getProcess_status().getProccessStatusId());
    List<String[]> results = rawResults.getResults();
    // This will select the first result (the first and maybe only row returned)
    String[] resultArray = results.get(0);
    //This will select the first field in the result which should be the ID
    result = resultArray[0];
} catch (Exception e) {
    e.printStackTrace();
}

希望这可以帮助。

于 2012-08-08T15:21:57.743 回答
6

如果不查看该领域的所有类别和其他类别,很难正确回答这个问题processStatusId。但是,我认为您正在做太多原始方法,并且可能无法正确逃避您的价值观等。

我建议您使用INSQL 语句而不是在循环中执行的操作。就像是:

List<String> ids = new ArrayList<String>();
for(Order r : LoginActivity.orders) {
    ids.add(r.getProcess_status().getProccessStatusId());
}
QueryBuilder<ProcessStatus, Integer> qb = dao.queryBuilder();
Where where = qb.where();
where.in(ProcessStatus.STATUS_ID, ids);
qb.selectColumns(ProcessStatus.STATUS_TITLE);

现在您已经构建了查询,您可以检索您的ProcessStatus对象,也可以使用以下方法获取标题dao.queryForRaw(...)

List<ProcessStatus> results = qb.query();
// or use the prepareStatementString method to get raw results
GenericRawResults<String[]> results = dao.queryRaw(qb.prepareStatementString());
// each raw result would have a String[] with 1 element for the title
于 2012-08-08T16:17:07.433 回答