dimanche 18 octobre 2015

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 forum very well, and saw that some people have the errors below

  1. Create table has typos in it, my create 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 once the column was added. I tried updating the database version 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 http://ift.tt/1hLJtfE i did check for all of the nuances it spoke 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 here http://ift.tt/1WIxCyU

Aucun commentaire:

Enregistrer un commentaire