2012-11-26 69 views
1

我有一個數據庫表,它有一個名爲「notes」的列。 根據特定行是否是我最喜歡的(添加到收藏夾),此列的數據只會獲得「是」或「否」。SQLite數據庫更新行時光標錯誤

我想用下面的代碼更新此列的數據:

public void updateFavorite(long id) 
    { 
     String txt; 
     System.out.println("first"); 
     Cursor c = database.query("recipes", null, "_id=" + id, null, null, null, null); 
     c.moveToFirst(); 
     System.out.println("second"); 
     int notesIndex = c.getColumnIndex("notes"); 
     System.out.println("third"); 
     String fav=c.getString(notesIndex); 
     System.out.println("fourth"); 

     System.out.println(fav); 

     if (fav=="yes") {txt="no";} 
     else {txt="yes";} 

     ContentValues editEsoda = new ContentValues(); 

     editEsoda.put("notes", txt); 

     open(); // open the database 
     database.update("recipes", editEsoda, "_id=" + id, null); 
     close(); // close the database 
     c.close(); 
    } 

我正在試圖獲取光標C時的錯誤:

Cursor c = database.query("recipes", null, "_id=" + id, null, null, null, null); 

我想讀的字符串在這一行和一列。如果是「是」,則將其轉爲「否」。否則,如果它是「否」,則將其轉爲「是」。

爲什麼我在那裏得到一個錯誤? 預先感謝您

這是我的logcat:

11-26 19:00:25.404: D/dalvikvm(30378): GC_FOR_MALLOC freed 4678 objects/265896 bytes in 79ms 
11-26 19:00:25.444: I/System.out(30378): first 
11-26 19:00:25.444: D/AndroidRuntime(30378): Shutting down VM 
11-26 19:00:25.444: W/dalvikvm(30378): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
11-26 19:00:25.454: E/AndroidRuntime(30378): FATAL EXCEPTION: main 
11-26 19:00:25.454: E/AndroidRuntime(30378): java.lang.IllegalStateException: Could not execute method of the activity 
11-26 19:00:25.454: E/AndroidRuntime(30378): at android.view.View$1.onClick(View.java:2072) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at android.view.View.performClick(View.java:2408) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at android.view.View$PerformClick.run(View.java:8816) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at android.os.Handler.handleCallback(Handler.java:587) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at android.os.Looper.loop(Looper.java:123) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at android.app.ActivityThread.main(ActivityThread.java:4627) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at java.lang.reflect.Method.invokeNative(Native Method) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at java.lang.reflect.Method.invoke(Method.java:521) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at dalvik.system.NativeStart.main(Native Method) 
11-26 19:00:25.454: E/AndroidRuntime(30378): Caused by: java.lang.reflect.InvocationTargetException 
11-26 19:00:25.454: E/AndroidRuntime(30378): at development.nk.cretanrecipes.ViewRecipes.favorite(ViewRecipes.java:235) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at java.lang.reflect.Method.invokeNative(Native Method) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at java.lang.reflect.Method.invoke(Method.java:521) 
11-26 19:00:25.454: E/AndroidRuntime(30378): at android.view.View$1.onClick(View.java:2067) 
11-26 19:00:25.454: E/AndroidRuntime(30378): ... 11 more 
11-26 19:00:25.454: E/AndroidRuntime(30378): Caused by: java.lang.NullPointerException 
11-26 19:00:25.454: E/AndroidRuntime(30378): at development.nk.cretanrecipes.DatabaseConnector.updateFavorite(DatabaseConnector.java:93) 
11-26 19:00:25.454: E/AndroidRuntime(30378): ... 15 more 
11-26 19:00:29.094: I/Process(30378): Sending signal. PID: 30378 SIG: 9 
+1

光標101:叫movetofirst才能訪問的行。 SO 101:發佈你的堆棧跟蹤。 SQLite 101:存儲布爾值爲整數(0或1) – njzk2

+0

我添加了c.moveToFirst();但我仍然得到錯誤。我發佈了我的LogCat。 –

+0

93是什麼? – njzk2

回答

1

試圖檢索Cursor c
Cursor c = database.query("recipes", null, "_id=" + id, null, null, null, null);

這意味着databasenull,你需要這行之前,使用database = helper.getWritableDatabase()和替換helperSQLiteOpenHelper變量時,我得到一個錯誤。我猜這是open()做的。

隨着什麼其他人說讓讓這個方法更快一點:

public void updateFavorite(long id) 
{ 
    open(); // open the database here! 
    ContentValues editEsoda = new ContentValues(); 

    // Only fetch the columns that you are going to work with 
    Cursor c = database.query("recipes", new String[] {"notes"}, "_id=" + id, null, null, null, null); 

    // If a row exists with the given id 
    if(c.moveToFirst()) { 
     // Toggle the value of "notes", but this should be a boolean 
     if(c.getString(0).equals("yes") 
      editEsoda.put("notes", "no"); 
     else 
      editEsoda.put("notes", "yes"); 

     c.close(); 
     database.update("recipes", editEsoda, "_id=" + id, null); 
    } 
    else { 
     // No data has this id 
    } 
    close(); // close the database 
} 
+1

哦,謝謝!當然,我不得不打開我的數據庫.... !!!我心何在 ? :)謝謝:) –

+0

很高興我能幫上忙。 – Sam

1
  • 首先,fav=="yes"是不正確的。您應該使用:"yes".equals(fav)
  • 其次,如果您只有兩個可能的值,爲什麼不將值存儲爲數據庫中的布爾值?您將節省存儲空間。
  • 三,你得到哪個錯誤??!
+0

我發佈了我的LogCat –