Android contentprovider complex query involving 2 tables - android

How do I run this query in a contentprovider?
SELECT * FROM sms_data WHERE number != (SELECT DISTINCT number from test) AND time > ?

First, the query is probably wrong. Instead of number != (...) you likely want number NOT IN (...).
Assuming test is a table in your app and not in the content provider, you can perform the query in two steps:
Subquery SELECT DISTINCT number FROM test. Build a comma-separated string such as '12345','23456','45678' from the results.
Do the content provider query with selection
number NOT IN (<that comma-separated list>) AND time > ?

For a subquery you can use rawQuery() or SQLiteQueryBuilder.

Overriding onQuery method of ContentProvider, I managed to put this bunch of code that works well for me.
#Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
switch (URI_MATCHER.match(uri)) {
case TABLE1:
Cursor cursor = database.query("sms_data", projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
case TABLE2:
Cursor c = database.query(true, "test", projection, selection, selectionArgs, null, null, sortOrder, null);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
case COMBINED:
String sql = "SELECT " + appendColumns(projection) +
" FROM sms_data WHERE number != IFNULL((SELECT DISTINCT number FROM test), 'abc') AND " + selection;
Cursor cur = database.rawQuery(sql, selectionArgs);
cur.setNotificationUri(getContext().getContentResolver(), uri);
return cur;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri + " Match = " + URI_MATCHER.match(uri));
}
}
Hope this works for you too.

Related

SQLiteDatabase IllegalArgumentException

I am having trouble querying the my SQLiteDatabase using my custom ContentProvider. I am trying to query the database for one user using the 'user_id'. The following pieces of code which are associated with the problem are:
DBProvider.java
#Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder){
int match_code = myUriMatcher.match(uri);
Cursor c;
switch (match_code){
case USER_BY_ID: {
c = dbHelper.getReadableDatabase().query(
DBContract.User_Table.TABLE_NAME,
projection,
DBContract.User_Table.COLUMN_ID + "='" + ContentUris.parseId(uri) + "'",
selectionArgs,
null,
null,
sortOrder
);
break;
}
default:
throw new UnsupportedOperationException("Not yet implemented");
}
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
ProfileActivity.java
// Columns to load
String[] columns = {
DBContract.User_Table.COLUMN_NAME // String "name"
};
// A cursor is your primary interface to the query results.
Cursor cursor = getActivity().getContentResolver().query(
ContentUris.withAppendedId(DBContract.User_Table.CONTENT_URI, Long.parseLong(id)), // Table to Query
columns, // Columns for the WHERE
DBContract.User_Table.COLUMN_ID, // selection
new String[]{"1"}, // Values for the WHERE
null // Sort Args
);
The error that I am getting is:
java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.
Any help would be greatly appreciated!
Thanks
You should use:
Cursor cursor = getActivity().getContentResolver().query(
ContentUris.withAppendedId(DBContract.User_Table.CONTENT_URI, Long.parseLong(id)), // Table to Query
columns, // Columns for the WHERE
DBContract.User_Table.COLUMN_ID, // selection
null, // Values for the WHERE
null // Sort Args
);
You are using the selectionArgs parameter wrong. You don't have any ? in the selection parameter and the binding fails. More info in the documentation.
selectionArgs - You may include ?s in selection, which will be
replaced by the values from selectionArgs, in the order that they
appear in the selection. The values will be bound as Strings.

How to display SQL query of a CursorLoader

I would like to display in the Log.d the current SQL query being generated by a CursorLoader in the onCreateLoader method such as:
return new CursorLoader(
getActivity(), // Parent activity context
WifiEntry.CONTENT_URI, // Provider content URI to query
projection, // Columns to include in the resulting Cursor
null, // No selection clause
null, // No selection arguments
WifiEntry.COLUMN_WIFI_NAME + " ASC");
I have looked at the Android docs, and in here, but no success. It is useful to debugging purposes to be able to visualize the SQL string.
Thanks!
UPDATE. I am adding the ContentProvider query method, anybody can answer how to display in the Log the resulting SQL query?
Thanks.
#Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder)
{
// Get readable database
SQLiteDatabase database = mDbHelper.getReadableDatabase();
// This cursor will hold the result of the query
Cursor cursor;
// Figure out if the URI matcher can match the URI to a specific code
int match = sUriMatcher.match(uri);
switch (match)
{
case WIFIS:
// For the WIFIS code, query the wifi table directly with the given
// projection, selection, selection arguments, and sort order. The cursor
// could contain multiple rows of the pets table.
cursor = database.query(WifiEntry.TABLE_NAME, projection, selection, selectionArgs,
null, null, sortOrder);
break;
case WIFI_ID:
// For the WIFI_ID code, extract out the ID from the URI.
// the selection will be "_id=?"
selection = WifiEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
cursor = database.query(WifiEntry.TABLE_NAME, projection, selection, selectionArgs,
null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Cannot query, unknown URI " + uri);
}
cursor.setNotificationUri(getContext().getContentResolver(), uri);
// Return the cursor
return cursor;
}

Fetch Date from database that is shipped with android app

I am following this tutorial http://mobisys.in/blog/2012/01/tutorial-using-database-in-android-applications/ to have an external database with my android app.
I am not able to apply where clause while fetching the data from the database.
This is what I have done so far.
c=myDbHelper.query("level1", null, "_id=2", null, null,null, null);
if(c.moveToFirst())
{
do {
Toast.makeText(Level1.this,
"_id: " + c.getString(0) + "\n" +
"name: " + c.getString(1) + "\n"
,
Toast.LENGTH_LONG).show();
} while (c.moveToNext());
}
The databaseHelper class
public Cursor query(String table,String[] columns, String selection,String[] selectionArgs,String groupBy,String having,String orderBy){
return myDataBase.query("level1", null, "_id", null, null, null, null);
}
Can anyone help me with this?
You pass "_id=2" as selection when calling myDbHelper.query. But in your DB Helper query method, selection is never used (same goes for other parameters).
You probably want to change query to something like:
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy){
// double check parameters I did type that here (no IDE)
return myDataBase.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
}
See also, the documentation of SQLiteDatabase

retrieve a single query from a data base in android by sqlite

I'm trying to retrieve a single row from my database using SQLite but for some reason my program crashes. When I search the log I get the next error:
Index -1 is requested with size of 1
I searched the web for solutions but it looks like my code is correct. I can delete a row with that parameter so I know that the position is right. It's probably how I write the query but I just don't know what's wrong with it. Can someone see why I'm doing wrong?
This is the code for the query:
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + /jokes_table");
final Uri _URI = Uri.parse(MyContentProvider.CONTENT_URI + "/2");
String positions = intent.getStringExtra("position_in_db");
Cursor cur = getBaseContext().getContentResolver().query(_URI, new String[] {"Joke","Author","Date","Status"} , MyContentProvider.ID + " = " + intent.getStringExtra("position_in_db") , null , null );
my query method :
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String a;
switch (sUriMatcher.match(uri))
{
case COLLECTION_URI_INDICATOR:
qb.setTables(TABLE_NAME);
qb.setProjectionMap(projectionMap);
break;
case SINGLE_ITEM_URI_INDICATOR:
qb.setTables(TABLE_NAME);
qb.setProjectionMap(projectionMap);
qb.appendWhere(ID);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, null);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
When I try to get all the rows by that query it works fine. The only problem is that I can't retrieve specific row. I try to change the selection with ? and try to see the string before I call the query but it won't work. Trying to reach data from the cursor by
cur.getString(getColumnIndex("Joke"));
ends the program. Can someone help me please?
What is your selection and selectionArgs??
you can try this
selection = ROW_ID_COLUMN_NAME + " =? "; // take a note on the "Space" between this statement
selectionArgs = { ROW_ID };
c = qb.query(db, projection, selection, selectionArgs, null, null, null);
// moveToFirst() method may prevent error when accessing 0 row cursor.
if (c.moveToFirst()) {
c.getString(getColumnIndex("Joke"));
}

Group By in ContentResolver in Ice Cream Sandwich

I am making a query on the Android Contacts ContentProvider. I need a Group By clause. In Gingerbread and Honeycomb, I do something like this to search phone numbers and emails at the same time:
(The actual WHERE clause is much more complicated as it includes types checks. This is a simplification, but it yields the same result)
String request = Phone.NUMBER + " LIKE ? OR " + Email.DATA + " LIKE ?";
String[] params = new String["%test%", "%test%"];
Cursor cursor = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data._ID, Data.RAW_CONTACT_ID },
request + ") GROUP BY (" + Data.RAW_CONTACT_ID,
params, "lower(" + Data.DISPLAY_NAME + ") ASC");
The injection of the ')' finishes the WHERE clause and allow the insertion of a GROUP BY clause.
However, in Ice Cream Sandwich, it appears that the ContentProvider detects this and adds the correct number of parenthesis to prevent my injection. Any other way of doing this in a single cursor query?
Edit
Currently, I have removed the GROUP BY, and added a MatrixCursor to limit the impact, but I'd rather have a real cursor:
MatrixCursor result = new MatrixCursor(new String[] { Data._ID, Data.RAW_CONTACT_ID });
Set<Long> seen = new HashSet<Long>();
while (cursor.moveToNext()) {
long raw = cursor.getLong(1);
if (!seen.contains(raw)) {
seen.add(raw);
result.addRow(new Object[] {cursor.getLong(0), raw});
}
}
I recently battled this issue querying the CallLog.Calls DB (where we were not able to modify the ContentProvider). What we ended up going with was building a query that looked like this:
SELECT _id, date, duration, type, normalized_number FROM calls WHERE _id IN (
SELECT _id FROM calls WHERE date < ? GROUP BY normalized_number ORDER BY date DESC LIMIT ?
);
The idea here is that we place any valid sqlite in our subquery, return a list of ids and then query again for all calls with those ids.
The final code looked something like this:
String whereClause = "_id IN (SELECT _id FROM calls WHERE data < ? GROUP BY normalized_number ORDER BY date DESC LIMIT ?)";
Cursor cursor = context.getContentResolver().query(
CallLog.Calls.CONTENT_URI,
new String[] { "_id", "date", "duration", "normalized_number" },
whereClause,
new String[]{ String.valueOf(amount), String.valueOf(dateFrom) },
null
);
...
In the case that you're querying for contacts, it would look something like this:
String whereClause = "_id IN (SELECT _id FROM contacts WHERE " + Phone.NUMBER + " LIKE ? OR " + Email.DATA + " LIKE ? GROUP BY " + Data.RAW_CONTACT_ID + " ORDER BY lower(" + Data.DISPLAY_NAME + ") ASC)";
String[] params = new String["%test%", "%test%"];
Cursor cursor = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data._ID, Data.RAW_CONTACT_ID },
whereClause,
params,
null
);
There will be some decrease in performance (since we're essentially querying twice for the same results), but it will surely be a lot faster than querying for all calls and doing the GROUP BY work in java world and also allows you to build up the query with additional clauses.
Hope this helps. We used this on Oreo and it fulfilled our needs.
You could create a custom Uri such that when your UriMatcher in your ContentProvider gets it, you can insert your group by clause and then execute the raw sql directly on the database.
first off all excuse my POOR English!
I'm new to Java/Android, started with 4.2.1 and fight with that too almost 2 days, then i start reading some more details about SQLiteQueryBuilder the query part is pretty much that what u are looking for ;)
it have:
public Cursor query (SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder)
the query "function" of the Content Provider only gives you:
query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder)
here u can trick around, i will post you my code snip:
#Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
/* a String is a Object, so it can be null!*/
String groupBy = null;
String having = null;
switch (sUriMatcher.match(uri)) {
...
...
...
case EPISODES_NEXT:
groupBy = "ShowID";
queryBuilder.setTables(EpisodenTable.TableName);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
Cursor c = queryBuilder.query(db, projection, selection, selectionArgs,
groupBy, having, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
thats its!
here the code i use to execute:
Cursor showsc = getContext().getContentResolver().query(
WhatsOnTVProvider.CONTENT_EPISODES_NEXT_URI,
EpisodenTable.allColums_inclCount,
String.valueOf(Calendar.getInstance().getTimeInMillis() / 1000)
+ " < date", null, null);

Categories

Resources