mercredi 24 février 2016

SQLiteException: no such column: category, but coulumn does exist

I'm creating an Android Project and I have a "DBFunc" class that has multiple methods to handles queries called by the activities.

DBFunc.java

public int getTotalNumberOfQuestions (String table, String category) {
    String selectQuery = "SELECT COUNT(*) FROM " + table + " WHERE category='" + category + "'";
    // example 
    // SELECT COUNT(*) FROM questions WHERE category='History';

    SQLiteDatabase database = this.getReadableDatabase();
    Cursor c = database.rawQuery(selectQuery, null);
    int ans = -1; // returns -1 if query unsuccessful
    if (c.moveToFirst()) {
        ans = c.getInt(0);
    }

    database.close();
    c.close();

    return ans;
}

I'm getting an error on the cursor, saying

android.database.sqlite.SQLiteException: no such column: category (code 1): , while compiling: SELECT COUNT(*) FROM questions WHERE category='Physics'

but I do have a category column in my questions table

When running this query through sqlite3 on the command prompt, it works and returns a number (e.g 1)

Here's what the schema looks like in "DB Browser for SQLite"

enter image description here

I really hope there's an easy solution, because I don't understand why it wouldn't work,

Thanks

Aucun commentaire:

Enregistrer un commentaire