我遇到了Android给我一个例外android.database.sqlite.SQLiteException: no such column _id
的问题。但奇怪的是我没有添加或正在搜索列_id。我将列id,title,release_date,vote_average,overview,poster_path添加到我的数据库中。我查看了我的DBHelper,主要活动和适配器,以验证_id是否在这些类中使用并且它一直给我错误android.database.sqlite.SQLiteException: no such column _id
。我想知道你们是否可以看看你是否能找到任何给我这个错误的东西?
谢谢你的帮助。
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)
主要活动:
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);
}
}