1

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

EDIT 1:

@CL asked for the code that creates the database. The database is created in sqlite3 command line and passed into the program. But the query I used was

CREATE TABLE questions (questionId INTEGER PRIMARY KEY AUTOINCREMENT, question TEXT, option1 TEXT, option2 TEXT, option3 TEXT, option4 TEXT, category TEXT);

EDIT 2:

I did what @Uwe Partzsch sugested and used LIKE instead of ' '

String selectQuery = "SELECT * FROM " + table + " WHERE category LIKE '" + category + "'";

But now I'm getting a different error

no such table: questions

EDIT 3:

public class DBFunc extends SQLiteOpenHelper {
    public static String DB_PATH ="/data/data/com.example.healyj36.quizapp/databases/";

    public static String DB_NAME = "questions.db";
    public static final int DB_VERSION = 1;
    public static final String TB_NAME1 = "questions";
    public static final String TB_NAME2 = "answers";

    private SQLiteDatabase myDB;
    private Context context;

    public DBFunc(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.context = context;
    }

    //Copy database from source code assets to device
    public void copyDataBase() throws IOException {
        try {
            InputStream myInput = context.getAssets().open(DB_NAME);
            String outputFileName = DB_PATH + DB_NAME;
            OutputStream myOutput = new FileOutputStream(outputFileName);

            byte[] buffer = new byte[1024];
            int length;

            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            myOutput.flush();
            myOutput.close();
            myInput.close();
        } catch (Exception e) {
            Log.e("tle99 - copyDatabase", e.getMessage());
        }
    }

    public void createDatabase() throws IOException {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            Log.e("tle99 - create", e.getMessage());
        }
    }
...
7
  • Did you add the category column after you first created your questions table? You can delete you app and install a fresh copy because everything looks good. Feb 24, 2016 at 18:11
  • Uninstall your app and install again.
    – Rohit5k2
    Feb 24, 2016 at 18:15
  • uninstalled the app and ran it again. still same error
    – healyj36
    Feb 24, 2016 at 18:22
  • @healyj36 Try String selectQuery = "SELECT COUNT(*) FROM " + table + " WHERE category like '" + category + "'";
    – Rohit5k2
    Feb 24, 2016 at 18:49
  • 1
    I'm too lazy to list all the bugs. Use SQLiteAssetHelper.
    – CL.
    Feb 25, 2016 at 12:51

3 Answers 3

1

This is can exists 2 possiblities:

  1. This problem can occurred from broken SQLite schema.

Your question is not about SQL Statement problem. but many developer can think SQL Statement problem about your question.

This case can check no demaged data in your database file. Although you can not use select fields and sql where clause by field name.

As a result, you can not use database file in your android code.

Exactly solution, I recommend recreate SQLite DB file, step by step.

You must be backup before use SQLite modification tool. (SQLite Manager, Browser, others db manage tools)

  1. This problem occurred from your persistent data.

If you use the same file name for assets or raw data when run with modified data,

you can try uninstall previous installed app for refresh.

0

Probably you have added the category column latter and trying to reinstall after modifications.

Either do a clear data from the Settings>app>your_app and launc

or

Uninstall the app and then install again.

0
0

Delete your app data, also you can try LIKE instead of '='.

String selectQuery = "SELECT * FROM " + table + " WHERE category LIKE '" + category + "'";
2
  • That's better, although now, I'm getting a different error. "no such table: questions"
    – healyj36
    Feb 25, 2016 at 9:49
  • Seems you did not create your tables. Can you show your SQL statement for table creation?
    – user2111879
    Feb 26, 2016 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.