0

I have looked through SO and found many people having this same error, but they seem to be forgetting to add the column into the create statement or missing whitespace, neither of which I believe I did.

My logcat is as follows:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gmd.referenceapplication, PID: 31988
android.database.sqlite.SQLiteException: no such column: QUANTITY (code 1): , while compiling: SELECT * FROM FTS WHERE (QUANTITY MATCH ?)
  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
  at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:400)
  at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:294)
  at com.gmd.referenceapplication.DatabaseTable.query(DatabaseTable.java:187)
  at com.gmd.referenceapplication.DatabaseTable.getWordMatches(DatabaseTable.java:179)
  at com.gmd.referenceapplication.SearchableActivity$1.onQueryTextChange(SearchableActivity.java:70)
  at android.support.v7.widget.SearchView.onTextChanged(SearchView.java:1150)
  at android.support.v7.widget.SearchView.access$2000(SearchView.java:103)
  at android.support.v7.widget.SearchView$12.onTextChanged(SearchView.java:1680)
  at android.widget.TextView.sendOnTextChanged(TextView.java:7991)
  at android.widget.TextView.handleTextChanged(TextView.java:8053)
  at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10157)
  at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1033)
  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:559)
  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:492)
  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:491)
  at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:685)
  at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:445)
  at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:340)
  at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5417)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

DatabaseTable class:

public static final String TAG = "ConstantDatabase";

//the columns included in the table
public static final String COL_QUANTITY = "QUANTITY";
public static final String COL_VALUE = "VALUE";
public static final String COL_UNCERTAINTY = "UNCERTAINTY";
public static final String COL_UNIT = "UNIT";
public static final String _id = "_id";
//name, tbale name, version
private static final String DATABASE_NAME = "CONSTANTS";
private static final String FTS_VIRTUAL_TABLE = "FTS";
private static final int DATABASE_VERSION = 1;


private final DatabaseOpenHelper mDatabaseOpenHelper;
private final Context mcontext;

public DatabaseTable(Context context){
    mDatabaseOpenHelper = new DatabaseOpenHelper(context);
    mcontext = context;
}

private  class DatabaseOpenHelper extends SQLiteOpenHelper {

    private final Context mHelperContext;
    private SQLiteDatabase mDatabase;
    private final MyDataProvider dp = new MyDataProvider(mcontext);

    private static final String FTS_TABLE_CREATE =
            "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE +
                    " USING fts3 (" +_id+ " INTEGER PRIMARY KEY,"+
                    COL_QUANTITY + " TEXT, " +
                    COL_VALUE + " TEXT," +
                    COL_UNCERTAINTY + " TEXT," +
                    COL_UNIT + " TEXT " + ")";

    public DatabaseOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        loadConstants();
        Log.e("Database Operation", "DatabaseOpenHelper constructor called, constants loaded?");
        mHelperContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        mDatabase = db;
        mDatabase.execSQL(FTS_TABLE_CREATE);
        Log.e("Database Operation", "Constants Table Created ...");
        loadConstants();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
        onCreate(db);
    }

    public SQLiteDatabase getmDatabase(){
        return mDatabase;
    }

    private void loadConstants() {
        new Thread(new Runnable() {
            public void run() {
                try {
                    loadConstantss();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
        Log.e("Loading", "Constants Table Populated ...");
    }

    private void loadConstantss() throws IOException {
        HashMap map = dp.getAllMap();
        Iterator<Map.Entry<String, ListViewItem>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, ListViewItem> entry = entries.next();
            Log.d("thing:", entry.getKey());
            //addConstant(entry.getKey(), entry.getValue(), entry.getUncertainty(), entry.getUnit());
        }
    }

    public long addConstant(String quantity, String value, String uncertainty, String unit) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues initialValues = new ContentValues();
        initialValues.put(COL_QUANTITY, quantity);
        initialValues.put(COL_VALUE, value);
        initialValues.put(COL_UNCERTAINTY, uncertainty);
        initialValues.put(COL_UNIT, unit);
        db.insert(FTS_VIRTUAL_TABLE, null, initialValues);
        return db.insert(FTS_VIRTUAL_TABLE, null, initialValues);
    }
    //database openhelper ends
}

public Cursor getWordMatches(String query, String[] columns) {
    String selection = COL_QUANTITY + " MATCH ?";
    String[] selectionArgs = new String[] {query+"*"};
    return query(selection, selectionArgs, columns);
}


public Cursor query(String selection, String[] selectionArgs, String[] columns) {
    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    builder.setTables(FTS_VIRTUAL_TABLE);

    Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(),
            columns, selection, selectionArgs, null, null, null);

    if (cursor == null) {
        return null;
    } else if (!cursor.moveToFirst()) {
        cursor.close();
        return null;
    }
    return cursor;
}

public Cursor getAllTitles(){
    return mDatabaseOpenHelper.getmDatabase().query(FTS_VIRTUAL_TABLE, new String[] {
                    COL_QUANTITY,
                    COL_UNCERTAINTY,
                    COL_UNIT,
                    COL_VALUE},
            null,
            null,
            null,
            null,
            null);
}

Thank you to anyone who can tell me why it is telling me the column "QUANTITY" is not being created, I really don't know.

guipivoto
  • 18,327
  • 9
  • 60
  • 75
grassss
  • 199
  • 1
  • 1
  • 15
  • Did you added that column recently? If did so, you must update the DATABASE version to force an "updating" and add that column... another option, is uninstall your app and install it again (or just erase its data) – guipivoto Jul 06 '16 at 21:00
  • if I'm just running it off of Android Studio, shouldn't that happen automatically? – grassss Jul 06 '16 at 21:03
  • If run before and added that colunm later, no... Thats why exists onUpgrade() method... to add columns later... but since you are debugging, it is easier uninstall and install the app aggain – guipivoto Jul 06 '16 at 21:18
  • ah thanks so much! what a dumb error – grassss Jul 06 '16 at 21:35

1 Answers1

2

If you change the schema you should increment DATABASE_VERSION so the database is upgraded.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134