2014-02-05 44 views
2

我完全失去了...這裏是我的代碼:內容提供商沒有這樣的表

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); 
    } 
} 

,這是開放幫手:

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) 

我浪費了整個晚上......據我所知,表沒有被創建。爲什麼它沒有被創建我不明白。當我重新安裝應用程序時,這是我不斷獲得的。

+0

**卸載**您的應用程序,以便刪除舊版本的數據庫,並且再次運行onCreate()。 – laalto

+0

它沒有幫助,它總是卡在queryBuilder.query()函數上。似乎打開的幫助程序在內容提供程序訪問它之前未創建表... –

+0

什麼是其他數據庫幫助程序,是否有可能使用相同的數據庫文件? – laalto

回答

4

根據註釋,您有兩個使用相同數據庫文件的數據庫幫助程序。以下是發生了什麼情況:

  1. 助手1數據庫在首次致電getWritableDatabase()時創建。助手的onCreate()被調用。數據庫文件的版本設置爲DATABASE_VERSION

  2. Helper 2的數據庫在打電話給其getWritableDatabase()時打開。數據庫文件已存在並且版本正確,因此不會調用onUpgrade()onCreate()。但是這個數據庫沒有helper 2的表格。

解決方案:每個數據庫文件只有一個數據庫幫助程序。你可以在一個助手中有多個表。