Android studio sqlite 查询意外错误
Android studio sqlite query unexpected error
在此查询中出现意外错误,我不知道如何解决这个问题,请帮助我。
我 运行 在 sql 浏览器中这个查询有效,但我不知道如何输入 android 格式方法
select PARTY,从 voter_table 中按 PARTY 分组计数(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;
}
试试这个
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 ;
我 运行 在 sql 浏览器中这个查询有效,但我不知道如何输入 android 格式方法 select PARTY,从 voter_table 中按 PARTY 分组计数(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;
}
试试这个
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 ;