Monday, December 3, 2012

Android - SQLite

Assuming a table called 'stocks' with a colum called 'items' that holds numeric data then the following may be what you are after: 

SQLiteDatabase db = WrenData.readable(); 
Cursor sum = db.query("stocks", "sum(items)", "", null, null, null, null); 
startManagingCursor(sum); 
sum.moveToNext(); 
result = sum.getLong(0); 
stopManagingCursor(sum); 

Cursor count = db.query("stocks", "count(*)", "", null, null, null, null); 
startManagingCursor(count); 
count.moveToNext(); 
result = count.getLong(0) 
stopManagingCursor(count); 

The variable 'result' in the above code is an int and 'db' is what would be returned by a call to SQLiteOpenHelper.getReadableDatabase(). The third parameter of the query is a where clause.