Question

I am inserting a ContentProvider layer in between the ORM of SugarORM and the underlying SQLite database in order to be able to use a SyncAdapter with it.

The ORM has a method like this:

public static <T extends SugarRecord<?>> List<T> find(Class<T> type,String whereClause,String[] whereArgs,String groupBy, String orderBy, String limit)

which matches nicely a query method of the SQLiteDatabase:

public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

the problem is that the ContentProvider only has one query method which doesn't match the necessary parameters:

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

So I was thinking of turning the parameters from the ORM into a SQL query, and then just passing that to my method, which can then run the raw query and return a cursor, like this:

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
{
    switch (uriMatcher.match(uri))
    {
    case RAW_QUERY:
        cursor = db.rawQuery(selection, selectionArgs);//query, arguments

...

the first issue is that this method of SQLiteQueryBuilder is deprecated:

buildQuery (String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit)

so then I tried this:

Object args[] = whereArgs;
String where_query = String.format(whereClause, args);
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(getTableName(type));
String query = builder.buildQuery(null, whereClause, whereArgs, groupBy, null, orderBy, limit);

but the resulting query string is null.

Any suggestions?

Was it helpful?

Solution

public Cursor query(Uri iUri, String[] iProjection, String iSelection,
            String[] iSelectionArgs, String iSortOrder) {
        SQLiteQueryBuilder mBuilder = new SQLiteQueryBuilder();
        mBuilder.setTables(Database.KEY_TABLE);
        switch (uriMatcher.match(iUri)) {
        case RAW_QUERY:
            mBuilder.appendWhere(Database.KEY_ROWID);
            break;
        default:
            throw new IllegalArgumentException("Unsupported URI: " + iUri);
        }
        Cursor cursor = mBuilder.query(db, iProjection, iSelection,
                iSelectionArgs, null, null, iSortOrder);
        return cursor;
    }

Try this in your code i think this will solve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top