List of usage examples for android.database.sqlite SQLiteQueryBuilder query
public Cursor query(SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs,
String groupBy, String having, String sortOrder)
From source file:de.nware.app.hsDroid.provider.onlineService2Provider.java
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor cursor = null;/*from w w w . j a v a 2 s . c o m*/ switch (mUriMatcher.match(uri)) { case EXAMS: SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); qb.setTables(mOpenHelper.getTableName()); qb.setProjectionMap(examsProjectionMap); SQLiteDatabase db = mOpenHelper.getReadableDatabase(); try { cursor = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder); } catch (SQLException e) { e.printStackTrace(); Log.d(TAG, "SqlError: " + e.getMessage()); } cursor.setNotificationUri(getContext().getContentResolver(), uri); break; case EXAMS_UPDATE: MatrixCursor cur = new MatrixCursor(EXAMS_UPDATE_COLUMNS); Integer[] columnValues = updateGrades(); cur.addRow(new Object[] { 0, columnValues[0], columnValues[1] }); return cur; case EXAMINFOS: cursor = getExamInfos(selectionArgs[0], selectionArgs[1], false); break; case CERTIFICATIONS: cursor = getCertifications(); break; default: throw new IllegalArgumentException("Unbekannte URI " + uri); } return cursor; }
From source file:org.mitre.svmp.common.DatabaseHandler.java
private Cursor _getConnectionInfoCursor(String selection, String... selectionArgs) { SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); queryBuilder/*from w ww .j a va2s. c om*/ .setTables(String.format("%s NATURAL LEFT JOIN %s", Tables[TABLE_CONNECTIONS], Tables[TABLE_APPS])); // prepared statement for speed and security return queryBuilder.query(getDb(), new String[] { "ConnectionID", "Description", "Username", "Host", "Port", "EncryptionType", "AuthType", "CertificateAlias", "COUNT(PackageName)", "Status" }, // columns (null == "*") selection, // selection ('where' clause) selectionArgs, // selection args "ConnectionID", // group by null, // having "Description" // order by ); }
From source file:org.mozilla.labs.Soup.provider.AppsProvider.java
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); qb.setTables(APPS_TABLE_NAME);// w w w . j a v a 2 s . c o m switch (sUriMatcher.match(uri)) { case APPS: qb.setProjectionMap(sAppsProjectionMap); break; case APP_ID: qb.setProjectionMap(sAppsProjectionMap); qb.appendWhere(Apps._ID + "=" + uri.getPathSegments().get(1)); break; case LIVE_FOLDER_APPS: qb.setProjectionMap(sLiveFolderProjectionMap); break; default: throw new IllegalArgumentException("Unknown URI " + uri); } // If no sort order is specified use the default String orderBy; if (TextUtils.isEmpty(sortOrder)) { orderBy = AppsContract.Apps.DEFAULT_SORT_ORDER; } else { orderBy = sortOrder; } // Get the database and run the query SQLiteDatabase db = mOpenHelper.getReadableDatabase(); Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy); // Tell the cursor what uri to watch, so it knows when its source data // changes c.setNotificationUri(getContext().getContentResolver(), uri); return c; }
From source file:com.gmail.emerssso.srbase.database.SRContentProvider.java
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selArgs, String sortOrder) { // Using SQLiteQueryBuilder instead of query() method SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); int uriType = sURIMatcher.match(uri); String tableName = ""; String[] columns;/*from ww w . j av a2s . co m*/ switch (uriType) { case SRS: case SR_ID: tableName = SRTable.TABLE_NAME; columns = SRTable.COLUMNS; break; case DAILIES: case DAILY_ID: tableName = DailyTable.TABLE_NAME; columns = DailyTable.COLUMNS; break; case PARTS: case PART_ID: tableName = PartTable.TABLE_NAME; columns = PartTable.COLUMNS; break; default: throw new IllegalArgumentException("Unknown URI: " + uri); } checkColumns(projection, columns); queryBuilder.setTables(tableName); if (isIdType(uriType)) { queryBuilder.appendWhere("_id =" + uri.getLastPathSegment()); } SQLiteDatabase db = database.getWritableDatabase(); Cursor cursor = queryBuilder.query(db, projection, selection, selArgs, null, null, sortOrder); // make sure that potential listeners are getting notified cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor; }
From source file:com.amazonaws.mobileconnectors.s3.transferutility.TransferDBBase.java
/** * Query records from the database./* www . ja va2s .c om*/ * * @param uri A Uri indicating which part of data to query. * @param projection The projection of columns. * @param selection The "where" clause of sql. * @param selectionArgs Strings in the "where" clause. * @param sortOrder Sorting order of the query. * @param type Type of transfers to query. * @return A Cursor pointing to records. */ public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); // TODO: currently all methods calling this pass null to projection. // In the future we want to update projection to be more specific for // performance and must handle that here. queryBuilder.setTables(TransferTable.TABLE_TRANSFER); final int uriType = uriMatcher.match(uri); switch (uriType) { case TRANSFERS: queryBuilder.appendWhere(TransferTable.COLUMN_PART_NUM + "=" + 0); break; case TRANSFER_ID: queryBuilder.appendWhere(TransferTable.COLUMN_ID + "=" + uri.getLastPathSegment()); break; case TRANSFER_PART: queryBuilder.appendWhere(TransferTable.COLUMN_MAIN_UPLOAD_ID + "=" + uri.getLastPathSegment()); break; case TRANSFER_STATE: queryBuilder.appendWhere(TransferTable.COLUMN_STATE + "="); queryBuilder.appendWhereEscapeString(uri.getLastPathSegment()); break; default: throw new IllegalArgumentException("Unknown URI: " + uri); } ensureDatabaseOpen(); final Cursor cursor = queryBuilder.query(database, projection, selection, selectionArgs, null, null, sortOrder); return cursor; }
From source file:edu.mit.mobile.android.locast.data.interfaces.LocatableUtils.java
public Cursor queryByLocation(SQLiteQueryBuilder qb, SQLiteDatabase db, String locString, String locatableItemTable, String[] projection, String selection, String[] selectionArgs, String sortOrder) {/*from w ww . ja v a2 s .co m*/ qb.setTables(locatableItemTable); final Matcher m = LOC_STRING_REGEX.matcher(locString); if (!m.matches()) { throw new IllegalArgumentException("bad location string '" + locString + "'"); } final String lon = m.group(1); final String lat = m.group(2); String dist = "1500"; if (m.groupCount() == 3) { dist = m.group(3); } final String cell = GeocellUtils.compute(new Point(Double.valueOf(lat), Double.valueOf(lon)), 8); final List<String> adjacent = GeocellUtils.allAdjacents(cell); adjacent.add(cell); final StringBuilder selSb = new StringBuilder(); boolean join = false; for (int i = 0; i < adjacent.size(); i++) { if (join) { selSb.append(" OR "); } else { join = true; } selSb.append(Locatable.COL_GEOCELL); selSb.append(" LIKE ? || '%'"); } final String selectionExtra = selSb.toString(); return qb.query(db, projection, ProviderUtils.addExtraWhere(selection, selectionExtra), ProviderUtils.addExtraWhereArgs(selectionArgs, adjacent.toArray(new String[] {})), null, null, sortOrder); // String extraWhere = // "(lat - 2) > ? AND (lon - 2) > ? AND (lat + 2) < ? AND (lat + 2) < ?"; // final String[] extraArgs = {lat, lon}; // return qb.query(db, projection, ProviderUtils.addExtraWhere(selection, // LocatableUtils.SELECTION_LAT_LON), ProviderUtils.addExtraWhereArgs(selectionArgs, // extraArgs), // null, null, sortOrder); }
From source file:com.android.talkback.labeling.LabelProvider.java
/** * Queries for a label or multiple labels in the labels database. * * @param uri The URI representing the type of query to perform: * {@code LABELS_CONTENT_URI} for a subset of all labels, * {@code LABELS_ID_CONTENT_URI} for a specific label, or * {@code PACKAGE_SUMMARY} for a label count per package. * @param projection The columns to return. * @param selection The WHERE clause for the query. * @param selectionArgs The arguments for the WHERE clause of the query. * @param sortOrder the ORDER BY clause for the query. * @return A cursor representing the data resulting from the query, or] * {@code null} if the query failed to execute. *///from w w w . j av a 2 s. c o m @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { if (uri == null) { LogUtils.log(this, Log.WARN, NULL_URI_FORMAT_STRING); return null; } if (!UserManagerCompat.isUserUnlocked(getContext())) { return null; } final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); queryBuilder.setTables(LabelsTable.TABLE_NAME); String groupBy = null; switch (sUriMatcher.match(uri)) { case LABELS: if (TextUtils.isEmpty(sortOrder)) { sortOrder = LabelsTable.KEY_ID; } break; case LABELS_ID: final String labelIdString = uri.getLastPathSegment(); final int labelId; try { labelId = Integer.parseInt(labelIdString); } catch (NumberFormatException e) { LogUtils.log(this, Log.WARN, UNKNOWN_URI_FORMAT_STRING, uri); return null; } final String where = String.format(Locale.ROOT, "%s = %d", LabelsTable.KEY_ID, labelId); queryBuilder.appendWhere(where); break; case PACKAGE_SUMMARY: projection = new String[] { LabelsTable.KEY_PACKAGE_NAME, "COUNT(*)" }; groupBy = LabelsTable.KEY_PACKAGE_NAME; sortOrder = LabelsTable.KEY_PACKAGE_NAME; break; default: LogUtils.log(this, Log.WARN, UNKNOWN_URI_FORMAT_STRING, uri); return null; } initializeDatabaseIfNull(); return queryBuilder.query(mDatabase, projection, selection, selectionArgs, groupBy, null /* having */, sortOrder); }
From source file:com.appsimobile.appsii.module.apps.AppsProvider.java
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); SQLiteDatabase db = mOpenHelper.getWritableDatabase(); // Generate the body of the query int match = sURLMatcher.match(uri); switch (match) { // loads all tagged-apps in a given tag. case TABLE_APPS_ITEM: long id = ContentUris.parseId(uri); qb.setTables(TaggedAppColumns.TABLE_NAME + "," + TagColumns.TABLE_NAME); qb.setProjectionMap(sAppsProjectionMap); qb.appendWhere(TaggedAppColumns.TAG_ID + "=" + id + " AND "); qb.appendWhere("taggedApps.tag_id=tags._id"); break;/* ww w . jav a 2 s . co m*/ // loads all tagged apps case TABLE_APPS: qb.setTables(TaggedAppColumns.TABLE_NAME + "," + TagColumns.TABLE_NAME); qb.setProjectionMap(sAppsProjectionMap); qb.appendWhere("taggedApps.tag_id=tags._id"); break; // loads items in the launch history table case TABLE_HISTORY: qb.setTables(LaunchHistoryColumns.TABLE_NAME); qb.setProjectionMap(sHistoryProjectionMap); break; // loads all tags case TABLE_TAGS: qb.setTables(TagColumns.TABLE_NAME); qb.setProjectionMap(sTagsProjectionMap); break; default: throw new IllegalArgumentException("Invalid uri: " + uri); } return qb.query(db, projection, selection, selectionArgs, null, null, sortOrder); }
From source file:com.appsimobile.appsii.module.home.provider.HomeContentProvider.java
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { selection = fixWhereProjection(uri, selection); SqlArguments args = new SqlArguments(uri, selection, selectionArgs); SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); String tables = createTablesFromTableQuery(args.table); qb.setTables(tables);/* ww w. j a v a 2 s. co m*/ Map<String, String> projectionMap = getProjectionMapForTable(args.table); if (projectionMap != null) { qb.setProjectionMap(projectionMap); qb.setStrict(true); } SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if (HomeContract.CELLS_TABLE_NAME.equals(args.table)) { sortOrder = fixSortOrder(sortOrder); } String where = args.where; if (HomeContract.HOTSPOT_PAGES_DETAILS_TABLE_NAME.equals(args.table)) { where = "_ht." + BaseColumns._ID + "=" + ContentUris.parseId(uri); } Cursor result = qb.query(db, projection, where, args.args, null, null, sortOrder); result.setNotificationUri(getContext().getContentResolver(), uri); return result; }
From source file:com.hivewallet.androidclient.wallet.AddressBookProvider.java
@Override public Cursor query(final Uri uri, final String[] projection, final String originalSelection, final String[] originalSelectionArgs, final String sortOrder) { final SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); qb.setTables(DATABASE_TABLE);// ww w.j a v a2s . com final List<String> pathSegments = uri.getPathSegments(); if (pathSegments.size() > 1) throw new IllegalArgumentException(uri.toString()); String selection = null; String[] selectionArgs = null; if (pathSegments.size() == 1) { final String address = uri.getLastPathSegment(); qb.appendWhere(KEY_ADDRESS + "="); qb.appendWhereEscapeString(address); } else if (SELECTION_IN.equals(originalSelection)) { final String[] addresses = originalSelectionArgs[0].trim().split(","); qb.appendWhere(KEY_ADDRESS + " IN ("); appendAddresses(qb, addresses); qb.appendWhere(")"); } else if (SELECTION_NOTIN.equals(originalSelection)) { final String[] addresses = originalSelectionArgs[0].trim().split(","); qb.appendWhere(KEY_ADDRESS + " NOT IN ("); appendAddresses(qb, addresses); qb.appendWhere(")"); } else if (SELECTION_QUERY.equals(originalSelection)) { final String query = '%' + originalSelectionArgs[0].trim() + '%'; selection = KEY_ADDRESS + " LIKE ? OR " + KEY_LABEL + " LIKE ?"; selectionArgs = new String[] { query, query }; } final Cursor cursor = qb.query(helper.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder); cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor; }