Android content provider content request

Is it possible to use getContentResolver().query() when I want sum(column) ?

OR

Should I make a raw request for the db descriptor?

+6
source share
4 answers

OK, it seems that its impossible to use getContentResolver().query() . I needed to establish a db connection and make rawQuery .

  ContentProviderClient client = getContentResolver().acquireContentProviderClient(AUTHORITY); SQLiteDatabase dbHandle= ((MyContentProvider)client.getLocalContentProvider()).getDbHandle(); Cursor cursor = dbHandle.rawQuery("SELECT sum("+COLUM_NNAME+") FROM "+TABLE_NAME +" WHERE "+WHERE_CLAUSE , null); cursor.moveToFirst(); int cnt = cursor.getInt(0); cursor.close(); cursor.deactivate(); client.release(); 
+3
source

When providing an array of columns before ContentResolver.query terminate the column name with the sum() function

 String[] columns = new String[] { "sum(" + columnName + ")" }; Cursor cursor = getContentResolver().query( content_uri, columns, selection, selectionArgs, sort ); cursor.moveToFirst(); int columnSum = cursor.getInt(0); 
+6
source

THE RESPONSE ACCEPTED INCORRECTLY

POSSIBLE IN SUPPLIER CONTENT

  String[] columns = new String[] { "sum(" + columnName + ")" }; Cursor cursor = getContentResolver().query( content_uri, columns, selection, selectionArgs, sort ); int columnSum = cursor.getInt(0); 

Only the mistake Tom made, he forgot to make:

 cursor.moveToFirst(); 
+3
source

You can use simpleQueryForLong () '-Method.

0
source

Source: https://habr.com/ru/post/887104/


All Articles