Skip to content Skip to sidebar Skip to footer

Select Distinct Not Working Android Sqlite

(NOTE question updated with what I have worked out so far) I am writing an app that has locations recorded by the following: id(primary), state, region, area, latitude, longitude

Solution 1:

SOLVED!

So SQLiteQueryBuilder methods that return a Cursor don't allow the Boolean parameter directly.

However after reading the class notes I noticed the method setDistinct and applied it to the SQLiteQueryBuilder.

Now it works and gives me just one unique entry for each state, just what I was trying to do. I did need to remove the other columns and just focus on the state, but thats another issue/solution not related to this question. Suffice to say I had to focus on just the column state and apply the setDistinct to that column only to get the desired result.

Heres the completed code:

public Cursor getData(){
    SQLiteDatabasedb= getReadableDatabase();
    SQLiteQueryBuilderqb=newSQLiteQueryBuilder();
    String [] sqlSelect = {"0 _id", "state"};
    StringsqlTables="Reefs";
    qb.setTables(sqlTables);
    qb.setDistinct(true);
    Cursorc= qb.query(db, sqlSelect, null, null, null, null, null);
    c.moveToFirst();
    return c;
}

Solution 2:

You have an errant 0 in this line:

String [] sqlSelect = {"0 _id", "state", "area", "region", "latitude", "longitude"};`
                        ^here

I suspect you didn't intend to select a literal value, so you should remove it:

String [] sqlSelect = {"_id", "state", "area", "region", "latitude", "longitude"};`

If you did intend to select the literal value, then you need to separate it with a comma:

String [] sqlSelect = {"0", "_id", "state", "area", "region", "latitude", "longitude"};`
                          ^here

Post a Comment for "Select Distinct Not Working Android Sqlite"