使用原始查询时,选择比较ormlite中两个列值的行

时间:2018-03-12 08:57:12

标签: android database android-studio sqlite ormlite

我有以下数据库表

@DatabaseTable(tableName = "Order_Details")
public class DB_OrderDetails {

@DatabaseField(generatedId = true)
private int id;

@DatabaseField()
private int order_id_from_order_table;

@DatabaseField()
private String medicine_code;

@DatabaseField()
private String medicine_name;

@DatabaseField()
private String medicine_type;

@DatabaseField()
private int number_of_units_ordered;

@DatabaseField()
private String generic_name;

@DatabaseField()
private String manufacturer_name;

@DatabaseField()
private double ordering_cost;
... }

我正在使用ormlite进行本地数据库操作。我想得到

的结果
SELECT * FROM Order_Details WHERE (medicine_code='xxxx' AND manufacturer_name='yyyy')

并希望在

中返回结果
List<DB_OrderDetails> 

任何人都可以建议我这样做吗?

1 个答案:

答案 0 :(得分:1)

使用 Where 类的对象来构建查询。

List<DB_OrderDetails> list;
try {
        Dao<DB_OrderDetails, String> dbOrderDetailsStringDao = dbHelper.getOrderDetailsDao();
        QueryBuilder<DB_OrderDetails, String> queryBuilder = dbOrderDetailsStringDao.queryBuilder();

        Where where = queryBuilder.where();
        where.eq("medicine_code", "xxxx");
        where.and();
        where.eq("manufacturer_name", "yyyy");

        PreparedQuery<DB_OrderDetails> preparedQuery = queryBuilder.prepare();
        list = dbOrderDetailsStringDao.query(preparedQuery);
} catch (SQLException e) {
        e.printStackTrace();
}