Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:08:49+00:00 2026-06-07T16:08:49+00:00

I am trying to limit the database result by defining a SQL WHERE clause

  • 0

I am trying to limit the database result by defining a SQL WHERE clause in the selection string of the query of the ContentResolver.

Cursor cursor = getContentResolver().query(uri, null, getSelectionString(), null, null);

…

public String getSelectionString() {
    // TODO Replace latitude and longitude with database reference.
    StringBuilder string = new StringBuilder();
    string.append("latitude >= ").append(northEast.getLatitudeE6() / 1e6);
    string.append(" AND ");
    string.append("latitude <= ").append(southWest.getLatitudeE6() / 1e6);
    string.append(" AND ");
    string.append("longitude >= ").append(southWest.getLongitudeE6() / 1e6);
    string.append(" AND ");
    string.append("longitude <= ").append(northEast.getLongitudeE6() / 1e6);
    return string.toString();
}

The database columns are defined as follows …

public class CustomDatabase {
    public static final class Contract {
        public static final String COLUMN_NAME = "name";
        public static final String COLUMN_LATITUDE = "latitude";
        public static final String COLUMN_LONGITUDE = "longitude";
    }
}

…

I am not particularly sure that I can inspect the query sent in cursor. If so, it does not contain the WHERE clause I sent:

SQLiteQuery: SELECT * FROM custom_db ORDER BY number ASC

Here is an example of the selection string:

latitude >= 48.203927 AND latitude <= 48.213851 AND longitude >= 16.36735 AND longitude <= 16.377648

Questions:

  1. Is the WHERE clause syntactically correct?
  2. Do I need apostrophs?
  3. Am I forced to use both parameters (selection, selectionArgs) at a time in a query?
  4. Where can I debug the whole query?

EDIT:

Here is the query() method of the ContentProvider …

public class CustomProvider extends ContentProvider {

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        switch (URI_MATCHER.match(uri)) {
        case URI_CODE_LOCATIONS:
            return mCustomDatabase.getLocations();
        }
        return null;
    }

… obviously, as biegleux guessed, I forgot to pass the parameters. Doh!
Here is the current implementation of the database method …

public class CustomSQLiteOpenHelper extends SQLiteOpenHelper {

    public Cursor getLocation() {
        return mDatabaseHelper.getReadableDatabase().query(
            CustomSQLiteOpenHelper.TABLE_NAME,
            null, null, null, null, null, null);
}

Do you suggest that I change the method signature to the following to pass all parameters? Am I not exposing to much of the database interface this way?

public Cursor getLocations(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
    return mDatabaseHelper.getReadableDatabase().query(
            CustomSQLiteOpenHelper.TABLE_NAME,
            columns, selection, selectionArgs, groupBy, having, orderBy);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:08:50+00:00Added an answer on June 7, 2026 at 4:08 pm

    Ok, so it seems query() method of your provider is causing problems.
    Make sure it looks like following.

    @Override
    public abstract Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    ...
    
    // run the query
    db.query(db, projection, selection, selectionArgs, groupBy, having, sortOrder, limit);
    

    You are not forced to use selectionArgs parameter, but with it code is more readable.
    To debug a query you can use

    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    Log.e(TAG, qb.buildQuery(projection, selection, selectionArgs, groupBy, having, sortOrder, limit);
    

    EDIT:
    If you have a ContentProvider implemented you don’t need to expose getLocations() method as you/users can use ContentProvider's query() method.
    You should pass at least those arguments whose can be passed in query() method and those are projection, selection, selectionArgs and sortOrder.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to query a single result from two tables in my database. Here
I'm trying to add a character limit clause to this white list regex approach
I have been trying to import Database through phpMyAdmin. My database file is a.sql
I'm trying to get a single result from my database, just one name. I
I am trying to paginate the results of an SQL query for use on
I'm currently trying to limit the memory resources of the process. Before to do
I am trying to limit connections and requests per IP then add a whitelist
I'm trying to limit what our users will be able to type in inputs,
I'm trying to limit the number of connections my server will accept using semaphores,
I'm trying to limit the number of records from an XML feed that are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.