【问题标题】:SQLite database Cursor error when updating a row更新行时SQLite数据库光标错误
【发布时间】:2012-11-26 16:47:14
【问题描述】:

我有一个数据库表,其中有一列名为“notes”。 此列的数据只会得到“是”或“否”,具体取决于特定行是否是我最喜欢的(添加到收藏夹)。

我正在尝试使用以下代码更新此列的数据:

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

【问题讨论】:

  • Cursor 101 :在访问一行之前调用 movetofirst。 SO 101:发布您的堆栈跟踪。 SQLite 101:将布尔值存储为整数(0 或 1)
  • 我添加了 c.moveToFirst();但我仍然得到错误。我发布了我的 LogCat。

标签: android database sqlite cursor


【解决方案1】:

我在尝试检索 Cursor c 时遇到错误
Cursor c = database.query("recipes", null, "_id=" + id, null, null, null, null);

这意味着databasenull,您需要在此行之前使用database = helper.getWritableDatabase() 并将helper 替换为您的SQLiteOpenHelper 变量。我猜这就是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
}

【讨论】:

  • 哦,谢谢!当然我必须先打开我的数据库.... !!!我的心思到哪去了 ? :) 谢谢 :)
【解决方案2】:
  • 首先,fav=="yes" 不正确。你应该使用:"yes".equals(fav)
  • 其次,如果您只有两个可能的值,为什么不将您的值作为布尔值存储在数据库中呢?您将节省存储空间。
  • 第三,你遇到了什么错误??!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多