2015-08-15 127 views
0

我正在爲android製作應用程序。我需要向數據庫中插入一定的值。Android插入不起作用

這是從DatabaseHelper(使用SQLite)的方法:

 public long insertMyAnime(MyList myList) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put("name", myList.getName()); 
     values.put("description", myList.getDescription()); 
     values.put("episodes", myList.getEpisodes()); 
     values.put("genre", myList.getGenre()); 
     values.put("rating", myList.getRating()); 
     values.put("episodesWatched", myList.getEpisodesWatched()); 
     values.put("stateId", myList.getStateId()); 

     long id = db.insert("myList", null, values); 

     db.close(); 
     return id; 
    } 

而這正是我要插入的部分:

public void addAnime_onClick(View v){ 
    anime = domParser.getAnime(name); 

    MyList myList = new MyList(); 

    myList.setName(anime.getName()); 
    myList.setDescription(anime.getDescription()); 
    myList.setEpisodes(anime.getEpisodes()); 
    myList.setGenre(anime.getGenre()); 
    myList.setEpisodesWatched(0); 
    myList.setStateId(4); 
    myList.setRating(0); 

    db.insertMyAnime(myList); 
} 

而這正是該表如下所示:

  //CREATE TABLE MYANIME 
     String CREATE_TABLE_MYLIST = "CREATE TABLE myList (" + 
       "id INTEGER PRIMARY KEY AUTOINCREMENT," + 
       "name TEXT," + 
       "description TEXT," + 
       "episodes INTEGER," + 
       "genre TEXT," + 
       "rating INTEGER," + 
       "episodesWatched INTEGER," + 
       "stateId INTEGER)"; 
     db.execSQL(CREATE_TABLE_MYLIST); 

從我按下按鈕插入時,應用程序崩潰。

你知道這個問題可能是什麼嗎?你會幫助我這麼多...

+0

那麼「插入不起作用」對你來說意味着什麼? – laalto

+0

正如我所說的,應用程序崩潰,「插入」並不難,所以我不認爲答案是遙遙領先的...... – Dominickus

+0

如果發生崩潰,那就是堆棧跟蹤。展示下。 –

回答

0

把它放在你的代碼中,看看你的LogCat。

try { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put("name", myList.getName()); 
    values.put("description", myList.getDescription()); 
    values.put("episodes", myList.getEpisodes()); 
    values.put("genre", myList.getGenre()); 
    values.put("rating", myList.getRating()); 
    values.put("episodesWatched", myList.getEpisodesWatched()); 
    values.put("stateId", myList.getStateId()); 

    long id = db.insert("myList", null, values); 

    db.close(); 
    return id;   
} catch (SQLiteException e) { 
    Log.e(TAG, "insert()", e); 
}