I have below sqlite query:
select product.title, supermarket.title,
datetime(price.timestamp,'localtime'), price.price
from price inner join product on price.productid = product._id
inner join supermarket on price.supermarketid = supermarket._id
where price.productid = 1
group by price.productid, price.supermarketid, price.timestamp
order by price.productid, price.supermarketid, price.timestamp
and I want to build the query with help of SQLiteOpenHelper so I am trying to translate it:
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// Define a projection that specifies which columns to read from the database
String[] projection = {
DBContract.Product.COLUMN_NAME_TITLE,
DBContract.Supermarket.COLUMN_NAME_TITLE,
"datetime(DBContract.Price.COLUMN_NAME_TIMESTAMP, 'localtime')",
DBContract.Price.COLUMN_NAME_PRICE
};
// Define 'where' part of query.
String selection = DBContract.Price.COLUMN_NAME_PRODUCT_ID + " LIKE ?";
// Specify arguments in placeholder order.
String[] selectionArgs = { String.valueOf(productId) };
// Define grouping by rows
String groupBy = DBContract.Price.COLUMN_NAME_PRODUCT_ID
.concat(DBContract.Price.COLUMN_NAME_SUPERMARKET_ID)
.concat(DBContract.Price.COLUMN_NAME_TIMESTAMP);
// Define order by clause
String orderBy = DBContract.Price.COLUMN_NAME_PRODUCT_ID
.concat(DBContract.Price.COLUMN_NAME_SUPERMARKET_ID)
.concat(DBContract.Price.COLUMN_NAME_TIMESTAMP);
Cursor cursor = db.query(
DBContract.Product.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
groupBy, // don't group the rows
null, // don't filter by row groups
orderBy // The sort order
);
My problems are:
I do not know if it is correct to specify below in the projection:
datetime(DBContract.Price.COLUMN_NAME_TIMESTAMP, 'localtime')
I do not know how to specify the inner joins using this syntax.
I know I can do it by building directly the query and execute it but I would like to do in this way so how could I get rid of this?
rawQueryinstead