ORMLite select some columns using predicates

久未见 提交于 2019-12-05 06:41:02

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/11866304/ormlite-select-some-columns-using-predicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!