ORMLite select some columns using predicates

2019-04-02 08:45发布

I have ORMLite database with some fields. I want to select titles from the table where id == id which I get from webservice. I do like that:

 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();
}

I tried like this but I get only sets of my id, not titles. I tried like this:

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

but I have the same result. How should I get data from database?

2条回答
在下西门庆
2楼-- · 2019-04-02 09:34

It's hard to properly answer this question without seeing all of the classes of the processStatusId field and others. However I think you are doing too much raw method and may not be properly escaping your values and the like.

I would recommend that you use the IN SQL statement instead of what you are doing in the loop. Something like:

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);

Now that you have built your query, either you can retrieve your ProcessStatus objects or you can get the titles themselves using 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
查看更多
冷血范
3楼-- · 2019-04-02 09:35

For selecting an specific field from the table, you could do something like this:

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();
}

Hope this helps.

查看更多
登录 后发表回答