- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我完全迷路了...这是我的代码:
public class MainContentProvider extends ContentProvider {
//DataBase
private WordsOpenHelper db_words;
private CategoryOpenHelper db_category;
//helper Strings to build the URI
private static String AUTHORITY = "com.ivanvoynov.dictionary.ContentProvider";
private static String BASE_PATH_WORDS = WordsOpenHelper.TABLE_NAME;
private static String BASE_PATH_CATEGORY = CategoryOpenHelper.TABLE_NAME;
//Content URI and the data types MIME
public static final Uri CONTENT_URI_WORDS = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH_WORDS);
public static final Uri CONTENT_URI_CATEGORY = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH_CATEGORY);
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + BASE_PATH_WORDS;
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + BASE_PATH_WORDS;
//our private variable to help match the URI types
private static final int ALL_ROWS_WORDS = 0;
private static final int ID_ROW_WORDS = 1;
private static final int ALL_ROWS_CATEGORY = 2;
private static final int ID_ROW_CATEGORY = 3;
//our URI matcher class to match our uri's
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static{
sUriMatcher.addURI(AUTHORITY, BASE_PATH_WORDS, ALL_ROWS_WORDS);
sUriMatcher.addURI(AUTHORITY, BASE_PATH_WORDS + "/#", ID_ROW_WORDS);
sUriMatcher.addURI(AUTHORITY, BASE_PATH_CATEGORY, ALL_ROWS_CATEGORY);
sUriMatcher.addURI(AUTHORITY, BASE_PATH_CATEGORY + "/#", ID_ROW_CATEGORY);
}
@Override
public boolean onCreate() {
db_words = new WordsOpenHelper(getContext());
db_category = new CategoryOpenHelper(getContext());
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
//create the query builder
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
//our database initialization
SQLiteDatabase database;
Cursor cursor;
//implement check column projection later on...
//implement all of them to return all with specific crap.
int uriType = sUriMatcher.match(uri);
switch (uriType){
case ALL_ROWS_WORDS:
//all words are asked
database = db_words.getWritableDatabase();
queryBuilder.setTables(WordsOpenHelper.TABLE_NAME);
cursor = queryBuilder.query(database, projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
case ALL_ROWS_CATEGORY:
//all words are asked
database = db_category.getWritableDatabase();
queryBuilder.setTables(db_category.TABLE_NAME);
cursor = queryBuilder.query(database, projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
case ID_ROW_WORDS:
//append to the table a new thing
queryBuilder.setTables(db_words.TABLE_NAME);
queryBuilder.appendWhere(db_words.KEY_ROW_ID + "=" + uri.getLastPathSegment());
database = db_words.getWritableDatabase();
cursor = queryBuilder.query(database, projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
case ID_ROW_CATEGORY:
//this is the category's row id selection
queryBuilder.setTables(db_category.TABLE_NAME);
queryBuilder.appendWhere(db_category.KEY_ROW_ID + "=" + uri.getLastPathSegment());
database = db_category.getWritableDatabase();
cursor = queryBuilder.query(database, projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
default:
throw new IllegalArgumentException("Unknown URI: " + uri + "URI TYPE = " + uriType);
}
}
这是 Open 助手:
public class WordsOpenHelper extends SQLiteOpenHelper{
//general info about the DataBase
public static final String DATABASE_NAME = "dictionary.db";
public static final String TABLE_NAME = "vocabulary";
private static final int DATABASE_VERSION = 1;
//DataBase columns
public static final String KEY_ROW_ID = "_id";
public static final String KEY_WORD = "word";
public static final String KEY_DEFINITION = "definition";
public static final String KEY_EXAMPLES = "examples";
public static final String KEY_DIFFICULTY = "difficulty"; //new term
public static final String KEY_SUBCATEGORY = "subcategory"; //new
public static final String KEY_COLOR = "color"; //new
public static final String KEY_LEARNED_FLAG = "learned_flag"; //new
public static final String KEY_ARCHIVED_FLAG = "archived_flag"; //new
public static final String KEY_TIMES_VIEWED = "times_viewed"; //count
public static final String KEY_TIMES_CORRECT = "times_correct"; //new coint
public static final String KEY_TIMES_INCORRECT = "times_incorrect"; //new
public static final String KEY_TIMES_SKIPPED = "times_skipped"; //new
public static final String KEY_LANGUAGE = "language"; //newone
public static final String KEY_SYNONYMS = "synonyms";
public static final String KEY_PART_OF_SPEECH = "part_of_speech";
public static final String KEY_CATEGORIES = "categories";
public static final String KEY_RECENT_FLAG = "recent_flag";
public static final String KEY_UNDEFINED_FLAG = "defined_flag";
//projection used in conjunction with other crap to form URI
public static final String[] PROJECTION = new String[]{
KEY_ROW_ID,
KEY_WORD,
KEY_DEFINITION,
KEY_DIFFICULTY,
KEY_EXAMPLES,
KEY_SYNONYMS,
KEY_PART_OF_SPEECH,
KEY_CATEGORIES,
KEY_RECENT_FLAG,
KEY_UNDEFINED_FLAG,
KEY_SUBCATEGORY,
KEY_COLOR,
KEY_LEARNED_FLAG,
KEY_ARCHIVED_FLAG,
KEY_TIMES_VIEWED,
KEY_TIMES_CORRECT,
KEY_TIMES_INCORRECT,
KEY_TIMES_SKIPPED,
KEY_LANGUAGE
};
//the table raw SQLite command to create it.
private static final String DICTIONARY_TABLE_CREATE =
"CREATE TABLE " + TABLE_NAME + " ("
+ KEY_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_WORD + " TEXT,"
+ KEY_DEFINITION + " TEXT,"
+ KEY_DIFFICULTY + " INTEGER,"
+ KEY_EXAMPLES + " TEXT,"
+ KEY_SYNONYMS + " TEXT,"
+ KEY_PART_OF_SPEECH +" TEXT, "
+ KEY_CATEGORIES + " TEXT, "
+ KEY_RECENT_FLAG + " TEXT,"
+ KEY_UNDEFINED_FLAG + " TEXT,"
+ KEY_SUBCATEGORY + " TEXT,"
+ KEY_COLOR + " TEXT,"
+ KEY_LEARNED_FLAG + " TEXT,"
+ KEY_ARCHIVED_FLAG + " TEXT,"
+ KEY_TIMES_VIEWED + " INTEGER,"
+ KEY_TIMES_CORRECT + " INTEGER,"
+ KEY_TIMES_INCORRECT + " INTEGER,"
+ KEY_TIMES_SKIPPED + " INTEGER,"
+ KEY_LANGUAGE + " TEXT "
+ ");";
public WordsOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DICTIONARY_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVerson, int newVersion){
//delete the table if it exists when upgrading
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
我一直收到这个错误:
Caused by: android.database.sqlite.SQLiteException: no such table: vocabulary (code 1): , while compiling: SELECT _id, word, definition, difficulty, examples, synonyms, part_of_speech, categories, recent_flag, defined_flag, subcategory, color, learned_flag, archived_flag, times_viewed, times_correct, times_incorrect, times_skipped, language FROM vocabulary
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:400)
at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:294)
at com.ivanvoynov.dictionary.ContentProvider.MainContentProvider.query(MainContentProvider.java:80)
我浪费了整个晚上... 据我了解,表尚未创建。为什么还没有创建我不明白。当我重新安装应用程序时,这就是我不断得到的。
最佳答案
根据评论,您有两个使用相同数据库文件的数据库助手。这是发生了什么:
Helper 1 数据库是在第一次调用 getWritableDatabase()
时创建的。助手的 onCreate()
被调用。数据库文件的版本设置为 DATABASE_VERSION
。
Helper 2 的数据库在调用其 getWritableDatabase()
时打开。数据库文件已存在且版本正确,因此不会调用 onUpgrade()
或 onCreate()
。但是这个数据库没有 helper 2 的表。
解决方案:每个数据库文件只有一个数据库助手。您可以在一个助手中拥有多个表。
关于android - Content Provider 没有这样的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21574800/
正在复制的问题是,当sew呈现登录页面时,然后我们继续执行身份验证,现在将我们重定向到网站的仪表板,此时我们继续关闭会话,并且在之前的交互中生成的这些cookie没有被删除。这个问题是重复性的,并且一
我是 Flutter 的新手,目前正在研究 DI。 我正在使用 flutter_bloc 和 provider 包。 flutter_bloc 附带一个 RepositoryProvider,我现在问
我是 Flutter 的新手,目前正在研究 DI。 我正在使用 flutter_bloc 和 provider 包。 flutter_bloc 附带一个 RepositoryProvider,我现在问
我正在使用 Angular2 开发一个应用程序。 我正在尝试在我的应用程序中使用 Reactive Forms,但我遇到了一些错误: 第一个错误是关于 NgControl 的,如下所示: No pro
最近很多用户在使用电脑的时候发现了wmi provider host进程占用内存比较大,不知道这个进程到底是干什么的,能不能禁止,怎么禁止。下面来一起看看想想的介绍吧。 wmi provide
我的问题是: 当我在设计时不知道这些表达式的数量和类型时,如何将列表中的表达式拼接成一个引用? 在底部,我包含了类型提供程序的完整代码。 (我已经剥离了这个概念来证明这个问题。)我的问题出现在这些行:
我目前正在学习使用 Flutter 进行应用程序开发,并已开始学习 Provider 包。我遇到了一些困难并收到错误: “在此...小部件之上找不到正确的提供者” 我最终移动了 Provider 小部
我是 android 的新手,我正在学习如何使用 JavaMail API 发送电子邮件的教程,我已经正确添加了必要的 Jar,但我总是遇到无法解析 GmailSender 类上的符号提供程序,我尝试
我正在我的 Angular 应用程序中进行单元测试,我正在使用 TestBed 方法, 我正在测试组件,所以每个规范文件看起来像这样 import... describe('AppComponent'
enter image description here 代码:这是我的 index.js 文件 index.js import { Provider } from "react-redux"
Microsoft ASP.NET Universal Providers 1.1昨天与System.Web.Providers 1.2一起发布.在后面的 nuget 页面上声明:Legacy pac
在我的 Next js 项目中,我使用了 Next auth,其中 import {Provider} from 'next-auth/client' , 并包裹 在 _app.js 中。 但是,与此
当我在 View 模型中使用如下界面时 class MainViewModel @ViewModelInject constructor( private val trafficImagesR
更新 - 我实际上发现它是 Flutter Issue . 我有两个 Provider,一个是 EntriesProvider,另一个是 EntryProvider。我在创建条目时使用我的 Entry
function configure($provide, $injector) { $provide.provider("testservice", function () {
这真让我抓狂。我似乎无法弄清楚这有什么问题。 代码: public interface IMinutesCounter { void startTimer(); void stopTi
我在我的项目中玩 Dagger 2,然后我陷入了这个错误编译。-> Error:(18, 21) error: ....MyManager cannot be provided without an
我有一个 Resteasy 应用程序,它使用 Spring 并包含 ContainerRequestFilter 和 ContainerResponseFilter 实现,并用 @Provider 注
我正在尝试使用 Dagger2 设置一个新项目,我以前使用过 Dagger2,但现在我正在尝试自己从头开始设置它。我正在从我参与的 Kotlin 项目中获取示例,但无法像现在在 Kotlin 中一样为
我刚开始学习 dagger2,遇到了一个奇怪的问题,在我看来像是一个错误。这是模块: @Module public class SimpleModule { @Provides Coo
我是一名优秀的程序员,十分优秀!