ORMLite使用predicates选择一些列 [英] ORMLite select some columns using predicates

查看:224
本文介绍了ORMLite使用predicates选择一些列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些领域ORMLite数据库。我想从其中id = = ID这是我从web服务获得表中选择标题。我做这样的:

 尝试{
    道< ProcessStatus,整数GT;道= db.getStatusDao();
    Log.i(状态,dao.queryForAll()的toString());
    QueryBuilder的< ProcessStatus,整数GT;查询= dao.queryBuilder();
    哪里哪里= query.where();
    字符串= NULL;
    对于(订购R:LoginActivity.orders){
        //LoginActivity.orders  - 阵列我的对象,我从web服务获得
        Log.i(数据库,query.selectRaw(选择process_status题)。
            其中,()。rawComparison(ProcessStatus.STATUS_ID,=,
                       。r.getProcess_status()getProccessStatusId())的toString())。
    }
    Log.i(SR,一);
}赶上(的SQLException E){
    // TODO自动生成的catch块
    e.printStackTrace();
}
 

我想是这样,但我得到的只有套我的ID,而不是头衔。我想是这样的:

  Log.i(数据库,query.selectColumns(ProcessStatus.STATUS_TITLE)。凡()。
    当量(ProcessStatus.STATUS_ID,r.getProcess_status()。getProccessStatusId())
    的ToString());
 

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

解决方案

 字符串结果=;
尝试 {
    GenericRawResults<的String []> rawResults = yourDAO.queryRaw(选择+
        ProcessStatus.STATUS_TITLE +从YourTable其中,+
        ProcessStatus.STATUS_ID +=+
        。r.getProcess_status()getProccessStatusId());
    名单<的String []>结果= rawResults.getResults();
    //这将选择的第一个结果(第一,也许只有一行返回)
    的String [] resultArray = results.get(0);
    //这将在其结果应该是ID选择的第一个字段
    结果= resultArray [0];
}赶上(例外五){
    e.printStackTrace();
}
 

这code可以帮助我很多,解决我的问题。由于 Zadec

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?

解决方案

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

That code helps me a lot to solve my question. Thanks to Zadec

这篇关于ORMLite使用predicates选择一些列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆