在android中删除sqlite中的一行

时间:2012-12-22 05:05:55

标签: android

在我的应用程序中我有一个数据库,因为我有1个包含3列的表。我想使用该表的id删除表bu中的一行..

我正在使用此代码:

public long DeleteStory(int rowId)
    {
        System.out.println("pos="+rowId);
        long del=db.delete(DATABASE_TABLE, KEY_ROWID + "="+rowId,null);
        System.out.println("edl="+del);
        return del;
    }

我的数据库包含数据:

id              heading                 Description
1               fgghsgahs               tyfgjhdfsdks

但它显示为nullpointer异常..我尝试了很多,但我无法解决它。帮我解决这个问题。提前谢谢。

我的logcat如下:

12-22 10:34:50.796: I/System.out(1207): pppppppp=1
12-22 10:34:50.816: I/System.out(1207): pos=1
12-22 10:34:50.816: D/AndroidRuntime(1207): Shutting down VM
12-22 10:34:50.816: W/dalvikvm(1207): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-22 10:34:50.816: E/AndroidRuntime(1207): Uncaught handler: thread main exiting due to uncaught exception
12-22 10:34:50.836: E/AndroidRuntime(1207): java.lang.NullPointerException
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.newspaper.DBadapter.DeleteStory(DBadapter.java:89)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.newspaper.SavedStoriesActivity.onOptionsItemSelected(SavedStoriesActivity.java:64)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.app.Activity.onMenuItemSelected(Activity.java:2170)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:525)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.view.View.onTouchEvent(View.java:4179)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.view.View.dispatchTouchEvent(View.java:3709)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.os.Looper.loop(Looper.java:123)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.app.ActivityThread.main(ActivityThread.java:4363)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at java.lang.reflect.Method.invoke(Method.java:521)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

LogCat显示此行抛出NullPointerException:

long del=db.delete(DATABASE_TABLE, KEY_ROWID + "="+rowId,null);

这意味着dbnull。您的数据库适配器中是否有open()方法?您需要在SQLiteOpenHelper对象上调用getWritableDatabase()来初始化db

答案 1 :(得分:1)

在进行数据库操作之前忘记初始化db,因此请将代码更改为:

public long DeleteStory(int rowId)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        System.out.println("pos="+rowId);
        long del=db.delete(DATABASE_TABLE, KEY_ROWID + "="+rowId,null);
        System.out.println("edl="+del);
         db.close();
        return del;
    }

有关删除,插入,更新数据库操作的更多帮助:

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/