Question

I tried to search for this but maybe I'm looking for it incorrectly.

This is for an android app. Essentially, I want to search the database based on some criteria (e.g. subcategory and item name), then use some of the other data values in the row to do a calculation (e.g. get the price value, and use it for a calculation)

I am thinking it is something like this query..

Cursor d = db.rawQuery("SELECT * FROM prices WHERE subcat = 'meat'  AND  item = 'lamb' ")

Now I would need to retrieve a column (like price) and use the values to calculate something like the average price.. How would I do that? (assuming step 1 is correct)...

Était-ce utile?

La solution

You can use aggregate functions to compute a single value based on a column's value in multiple records:

SELECT AVG(price) FROM prices WHERE subcat = 'meat' AND item = 'lamb'

Autres conseils

After creating your cursor, get the index(es) of the column(s) you want to read. For example, if one of the columns returned by SELECT * is price, the following gives the position of that column in each cursor row:

int PRICE_INDEX = d.getColumnIndexOrThrow("price");

Then you need to iterate over the results set. For each row, use the appropriate 'get' method to extract the values. Here I've assumed price is an INTEGER on the database, hence getInt():

while (d.moveToNext())
{
    int price = d.getInt(PRICE_INDEX);
    //Do whatever calculation you need with price here, or add to an array to process later
}

Remember to close the cursor when you are finished with it:

d.close();

If you can do the calculation within the SQL, then CL's answer is better. In that case, only one row is returned so no need for a loop - just use d.MoveToFirst() and then d.getInt(0). You can safely do getInt(0) because you are only returning a single column in the SELECT.

Create a class for your product say

Class AnimalProduct{
 Sting cat, subcat, item;
 int price;}

then run query in your database(I assume you have columns cat, subcat, item, price) based on your criteria say items = "lamb"

public ArrayList<AnimalProduct> getItem(String item) 
{

    ArrayList<AnimalProduct> aps = new ArrayList<AnimalProduct>();
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(YOURTABLE, 
            KEY_CAT, KEY_SUBCAT, KEY_ITEM, KEY_PRICE, 
            KEY_ITEM + " = ?" , new String[] {item }, 
            null, null, null, null);

    if (cursor.moveToFirst()) 
    {
        cursor.moveToLast();
        do {                
            AnimalProduct t = new AnimalProduct();

            t.cat       = cursor.getString(0);
            t.subcat    = cursor.getString(1);
            t.item   = cursor.getString(2);
            t.price = Integer.parseInt(cursor.getString(3));

            tList.add(t);
        } while (cursor.moveToPrevious());
    }

    cursor.close();
    db.close();         
    return aps; 
}

Once you get this list in your code, you will easily able to make whatever calculations required.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top