Search code examples
androidsqliteandroid-contentprovider

SQLiteException: no such column Android


I am trying to create a SQLite database for my Android app.
Everything worked fine until I got to the JUnit Testing for the query function in ContentProvider.

I read the forum very in depth, and saw that some people have the errors below

  1. Create table has typos in it - here's my table creation statement

    CREATE TABLE movie (
    _id INTEGER PRIMARY KEY,
    title TEXT NOT NULL,
    overview TEXT DEFAULT 'NO OVERVIEW',
    poster_path TEXT DEFAULT 'NO POSTER',
    release_date TEXT DEFAULT 'NO DATE AVAILABLE',
    vote_average TEXT DEFAULT 'NO VOTES YET',
    sort_type INTEGER NOT NULL,
    favorite INTEGER DEFAULT 0
    );

  2. Not updated Database_Version constant once the column was added.
    I tried updating the Database_Version constant and I also tried changing the name of the database, so it is created from scratch.

  3. Deleted all of my old app from my Android device.

  4. Read this post.
    I did check for all of the nuances it speaks about.

However, I still have my exception being thrown

android.database.sqlite.SQLiteException: no such column: MovieContract.Movie.favorite (code 1): , while compiling: SELECT * FROM movie WHERE MovieContract.Movie.favorite = ? ORDER BY MovieContract.Movie.title

My testCase method that throws the error.
Error is being thrown on the line Cursor movieCursor...

public void testBasicMovieQuery(){
    MovieDBHelper dbHelper = new MovieDBHelper(mContext);
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    ContentValues movieValues = TestUtilities.createMovieValues();
    long recordNum = db.insert(MovieContract.Movie.TABLE_NAME, null,movieValues);
    assertTrue("Unable to Insert WeatherEntry into the Database", recordNum != -1);
    db.close();

    String selection = "MovieContract.Movie.FAVORITE = ?";
    String [] selectionArgs = new String [] {"'1'"};
    String sortOrder = "MovieContract.Movie.TITLE";



    Cursor movieCursor = mContext.getContentResolver().query(
            MovieContract.Movie.CONTENT_URI,
            null,
            selection,
            selectionArgs,
            sortOrder
    );
    TestUtilities.validateCursor("testBasicWeatherQuery", movieCursor, movieValues);

    movieCursor.close();
}

Here is my query method in my ContentProvider; so when I have 'selection' defined it throws me the 'no such column' but if I put all null, besides the URI it will throw the Unknown Uri exception from the default, even though the Uri actually exists in UriMatcher.

    @Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    Cursor cursor;
    Log.v("QUERY MovieProvider", uri.toString());
    switch (uriMathcher.match(uri)){
        case MOVIE_WITH_ID:{
            Log.v("MovieProvider QUERY", "MOVIE WITH ID");
            //cursor = getMovieWithId(uri);
            cursor = dbHelper.getReadableDatabase().query(MovieContract.Movie.TABLE_NAME ,null, "MovieContract.Movie._ID =", selectionArgs,null,null,sortOrder);
        }
        break;

        case MOVIE:{
            Log.v("MovieProvider QUERY", "MOVIE");
            //Log.v("MovieProvider QUERY", selection);
            //Log.v("MovieProvider QUERY", selectionArgs[0]);
            cursor = dbHelper.getReadableDatabase().query(MovieContract.Movie.TABLE_NAME, null,selection, selectionArgs, null, null, sortOrder);
        }
        default: {
            throw new UnsupportedOperationException("Unknown uri: " + uri);
        }
    }
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}

Please let me know if any additional information is required.
My Github repository is here


Solution

  • Please change these lines:

    String selection = "MovieContract.Movie.FAVORITE = ?";
    String [] selectionArgs = new String [] {"'1'"};
    String sortOrder = "MovieContract.Movie.TITLE";
    

    to

    String selection = MovieContract.Movie.FAVORITE + " = ?";
    String [] selectionArgs = new String [] {"1"};
    String sortOrder = MovieContract.Movie.TITLE;
    

    or to (will work as well)

    String selection = "favorite = ?";
    String [] selectionArgs = new String [] {"1"};
    String sortOrder = "title";