我正在尝试为我的Android应用创建一个SQLite数据库 一切正常,直到我在ContentProvider中进行JUnit Testing的查询功能。
我非常深入地阅读了论坛,并看到有些人有以下错误
创建表中有拼写错误 - 这是我的表创建语句
CREATE TABLE电影(
_id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
概述文本默认'没有概述',
poster_path文字默认'没有海报',
release_date文字默认'没有日期可用',
vote_average文字默认'没有投票',
sort_type INTEGER NOT NULL,
最喜欢的INTEGER DEFAULT 0
);
添加列后未更新Database_Version常量 我尝试更新Database_Version常量,我也尝试更改数据库的名称,因此它是从头开始创建的。
从我的Android设备上删除了所有旧应用。
阅读this post。
我确实检查了它所说的所有细微差别。
但是,我仍然会抛出异常
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
我的testCase方法抛出错误。
在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();
}
这是我的ContentProvider中的查询方法;因此,当我定义'selection'时,它会抛出'no such column',但是如果我把所有null放在一边,除了URI之外,它会从默认值中抛出Unknown Uri异常,即使Uri实际上存在于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;
}
如果需要任何其他信息,请与我们联系 我的Github存储库是here
答案 0 :(得分:1)
请更改这些行:
String selection = "MovieContract.Movie.FAVORITE = ?";
String [] selectionArgs = new String [] {"'1'"};
String sortOrder = "MovieContract.Movie.TITLE";
到
String selection = MovieContract.Movie.FAVORITE + " = ?";
String [] selectionArgs = new String [] {"1"};
String sortOrder = MovieContract.Movie.TITLE;
或(也将起作用)
String selection = "favorite = ?";
String [] selectionArgs = new String [] {"1"};
String sortOrder = "title";