0

I'm running into an issue where Android is giving me an Exception android.database.sqlite.SQLiteException: no such column _id. But the weird part is that I did not add or am I searching for column _id. I am adding columns id, title, release_date, vote_average, overview, poster_path into my database. I looked through my DBHelper, Main Activity and Adapter to verify _id is not use in those classes and it keeps giving me errors android.database.sqlite.SQLiteException: no such column _id. I was wondering if you guys can take a look and see if you can find anything that is giving me this error?

Thanks for you help.

LOGCAT:

java.lang.RuntimeException: Unable to start activity ComponentInfo{Kavin.Ha.Movie.App/com.example.kh870h.moviediscovery.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist at com.example.kh870h.moviediscovery.SaveMovieData.FavoriteMoviesAdapter.<init>(FavoriteMoviesAdapter.java:0) at com.example.kh870h.moviediscovery.MainActivity.onCreate(MainActivity.java:77)

Main Activity:

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    moviesList = (GridView) findViewById(R.id.grid_view);

    //construct a custom adapter to attach it to our gridview
    movieAdapter = new MovieAdapter(MainActivity.this, R.layout.activity_gridview, new ArrayList<MovieItem>());
    moviesList.setAdapter(movieAdapter);

    getSupportLoaderManager().initLoader(CONTACT_LOADER_ID, new Bundle(), favoriteMoviesLoader);


    //initialize the adapter
    FavoriteMoviesDBHelper favoriteMoviesDBHelper = new FavoriteMoviesDBHelper(this);
    SQLiteDatabase db = favoriteMoviesDBHelper.getWritableDatabase();
    String request = "SELECT title FROM results WHERE title = ?";
    Cursor cursor = db.rawQuery(request, new String[] {KEY_TITLE + ""});
    FavoriteMoviesAdapter favoriteMoviesAdapter = new FavoriteMoviesAdapter(this, cursor);
    moviesList.setAdapter(favoriteMoviesAdapter);

    if (isOnline()) {
        new movieData().execute(POPULAR_MOVIE);
    }
}

DBHelper :

@Override
    public void onCreate(SQLiteDatabase db) {
    final String SQL_CRETE_MOVIES_FAVORITE =
            "CREATE TABLE " + FAVORITE_MOVIE_DB_TABLE_NAME + " (" +

                FavoriteMoviesEntry.KEY_ID          + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                FavoriteMoviesEntry.KEY_TITLE       + " TEXT NOT NULL, " +
                FavoriteMoviesEntry.KEY_RELEASE     + " INTEGER NOT NULL, " +
                FavoriteMoviesEntry.KEY_AVERAGE     + " INTEGER NOT NULL, " +
                FavoriteMoviesEntry.KEY_OVERVIEW    + " TEXT NOT NULL, " +
                FavoriteMoviesEntry.KEY_POSTER_PATH + " TEXT NOT NULL, " +
                " UNIQUE (" + FavoriteMoviesEntry.KEY_OVERVIEW + ") ON CONFLICT REPLACE);";

    //execute the query by calling execSQL
    db.execSQL(SQL_CRETE_MOVIES_FAVORITE);
}

ADAPTER:

public FavoriteMoviesAdapter(Context context, Cursor c)
{
    super(context, c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.activity_gridview, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    //setting the images and textviews so it can be stored
    ImageView imageView = (ImageView) view.findViewById(R.id.imageView);

    String[] projection = new String[] {
            KEY_ID,
            KEY_TITLE,
            KEY_RELEASE,
            KEY_AVERAGE,
            KEY_OVERVIEW,
            KEY_POSTER_PATH
    };

    //fetching the data using cursor
    String id = cursor.getString(0);
    String title = cursor.getString(1);
    String release_date = cursor.getString(2);
    String vote_average = cursor.getString(3);
    String overview = cursor.getString(4);
    String poster_path = cursor.getString(5);

    //store it into the database
    ContentValues values = new ContentValues();
    values.put("id", id);
    Log.d("id inside db", "id: " + id);
    values.put("title", title);
    Log.d("title inside db", "title: " + title);
    values.put("release_date", release_date);
    Log.d("release_date inside db", "release_date: " + release_date);
    values.put("vote_average", vote_average);
    Log.d("vote_average inside db", "vote_average: " + vote_average);
    values.put("overview", overview);
    Log.d("overview inside db", "overview: " + overview);
    values.put("poster_path", poster_path);
    Log.d("poster_path inside db", "poster_path: " + poster_path);
    //store the data into the database
    context.getContentResolver().insert(CONTENT_URI, values);

    //request data from database table
    cursor = context.getContentResolver().query(FavoriteMoviesContract.FavoriteMoviesEntry.CONTENT_URI,
            projection,
            null,
            null,
            null);
    cursor.moveToFirst();
        StringBuilder res = new StringBuilder();
    while (!cursor.isAfterLast()) {
        //retrieve the data using the cursor
        res.append(cursor.getString(cursor.getColumnIndex(FAVORITE_MOVIE_DB_TABLE_NAME)));
        cursor.moveToNext();
    }

    Picasso.with(context).load("http://image.tmdb.org/t/p/w500/" + cursor.getString(4))
            .error(R.drawable.ic_error_placeholder)
            .into(imageView);
}

}
kav
  • 165
  • 4
  • 12
  • `CursorAdapter` requires that the `Cursor` have an `_id` column. Yours doesn't, which is why you get that `IllegalArgumentException`. If you don't want to rename your `id` column to `_id`, you can alias it in the query; e.g. `SELECT id as _id, title FROM ...`. – Mike M. Feb 08 '18 at 00:00
  • 1
    Thanks Mike I believe this have resolve this issue. – kav Feb 08 '18 at 00:23

0 Answers0