在SQL查询上崩溃Android应用程序

时间:2014-01-16 19:26:40

标签: java android sql

在这个函数中,我试图获取表格中的行数。

public int getSetCount(){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT " +KEY_SET_ID+" * FROM "+TABLE_SETS;
    Cursor mcursor = db.rawQuery(query, null);
    mcursor.moveToFirst();
    return mcursor.getInt(0);

}

logCat打印出类似这样的内容

   sqlite returned: error code = 1, msg = near "FROM": syntax error

在这个函数中,我将找到具有给定ID的名称。

public String getSetName (int ID){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM "+TABLE_SETS+" WHERE "+KEY_SET_ID+" =? "+ID, null);
    if (cursor != null)
        cursor.moveToFirst();
    return cursor.getString(0);
}

这些功能有什么问题,请告诉我?根据所有论坛和教程,我正在做正确的事情。

1 个答案:

答案 0 :(得分:3)

您的SQL语句似乎未正确编写。只需更改以下行:

   String query = "SELECT " +KEY_SET_ID+" * FROM "+TABLE_SETS;

为:

String query = "SELECT " +KEY_SET_ID+" FROM "+TABLE_SETS;