ActiveAndroid
ActiveAndroid copied to clipboard
custom query with multi column result
Hello.
I cannot understand how i make custom query which returns me n-tuple result.
Example:
select sum(col1), max(col1) from...
Seems like there is no such methods in SQLiteUtils
if you want sum function. You can do like this:
SQLiteDatabase mydatabase = openOrCreateDatabase("dbname.db",MODE_PRIVATE,null);
Cursor cursor = mydatabase.rawQuery(“select sum(column1) as column1 from myTable”, null);
cursor.moveToLast();
Log.i(TAG, “sum of column1 is : ” + cursor.getInt(cursor.getColumnIndex("column1”));
Another example, using ActiveAndroid specifically to get the database:
String query = "SELECT SUM( file_size ) as file_size_sum FROM files";
Cursor cursor = ActiveAndroid.getDatabase().rawQuery( query, null );
cursor.moveToFirst();
long fileSizeSum = cursor.getInt( 0 );