0

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jenny
  • 445
  • 2
  • 6
  • 23
  • The content provider fields are case sensitive. `MovieContract.Movie.FAVORITE` is not the same as `MovieContract.Movie.favorite` – t0mm13b Oct 18 '15 at 15:57
  • @t0mm13b Hmmm, never new about it, but point taken. I changed them to lower case but it still does not solve the problem. I updated the error message above – Jenny Oct 18 '15 at 16:08
  • try to uninstall you app from your device and reinstall it again – Mounir Elfassi Oct 18 '15 at 16:10
  • ok, something is amiss.. btw, noticed [this](https://github.com/JennyCh/Popular_Movies_S1/blob/master/app/src/main/java/com/example/jenny/popular_movies_s1/Movie.java) the order of write must match up with order of read when implementing `Parcelable`. – t0mm13b Oct 18 '15 at 16:13
  • @t0mm13b I fixed it, thanx – Jenny Oct 18 '15 at 16:21
  • @MounirElfassi I did (#3) in my post, besides I don't actually have an app on a device since its JUnit testing it not being installed – Jenny Oct 18 '15 at 16:23
  • @t0mm13b sorry I hope my previous comment is not misleading, I fixed the order PROBLEM STILL NOT FIXED. – Jenny Oct 18 '15 at 16:40

1 Answers1

1

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";
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115