Search code examples
androidsqliteandroid-studioandroid-sqlite

Android studio sqlite query unexpected error


screenshotgot a unexpected error in this query, I don't know how to solve this problem, please help me anyone.

I run in sql browser this query is working, but I don't know how enter android format method select PARTY, count(PARTY) from voter_table group by PARTY;

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "Vote.db";
    public static final String TABLE_NAME = "voter_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "PARTY";
    public static final String COL_3 = "USERNAME";
public int getCountData() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res1 = db.rawQuery("select" +COL_2+", count("+COL_2+") from" +TABLE_NAME+ "group by" +COL_2;);
        res1.moveToFirst();
        int recCount = res1.getInt(0);
        res1.close();
        db.close();
        return recCount;
    }

Solution

  • Try This

    static final String TABLE_NAME = "voter_table";
        public static final String COL_1 = "ID";
        public static final String COL_2 = "PARTY";
        public static final String COL_3 = "USERNAME";
        String query = "SELECT " +COL_2 +",COUNT(" +COL_2 +") FROM " + TABLE_NAME + " GROUP BY " + COL_2 ;