我的应用程序有两个按钮,第一个按钮用于删除用户输入的记录,第二个按钮用于删除所有记录。但是当我想删除数据时,它会显示消息 “您的应用程序已被强制停止”。
请检查我的代码并给我一些建议。
public void deleteAll() { //SQLiteDatabase db = this.getWritableDatabase(); // db.delete(TABLE_NAME,null,null); //db.execSQL("delete * from"+ TABLE_NAME); db.execSQL("TRUNCATE table" + TABLE_NAME); db.close(); }
和
public void delete(String id) { String[] args={id}; getWritableDatabase().delete("texts", "_ID=?", args); }
但它显示以下 Log cat 错误。
03-07 15:57:07.143: ERROR/AndroidRuntime(287): Uncaught handler: thread main exiting due to uncaught exception 03-07 15:57:07.153: ERROR/AndroidRuntime(287): java.lang.NullPointerException 03-07 15:57:07.153: ERROR/AndroidRuntime(287): at com.example.MySQLiteHelper.delete(MySQLiteHelper.java:163) 03-07 15:57:07.153: ERROR/AndroidRuntime(287): at com.example.Settings$4.onClick(Settings.java:94) -07 15:57:07.153: ERROR/AndroidRuntime(287): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) 03-07 15:57:07.153: ERROR/AndroidRuntime(287): at android.os.Handler.dispatchMessage(Handler.java:99) 03-07 15:57:07.153: ERROR/AndroidRuntime(287): at android.os.Looper.loop(Looper.java:123) 03-07 15:57:07.153: ERROR/AndroidRuntime(287): at android.app.ActivityThread.main(ActivityThread.java:4203) 03-07 15:57:07.153: ERROR/AndroidRuntime(287): at java.lang.reflect.Method.invokeNative(Native Method) 03-07 15:57:07.153: ERROR/AndroidRuntime(287): at java.lang.reflect.Method.invoke(Method.java:521) 03-07 15:57:07.153: ERROR/AndroidRuntime(287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 03-07 15:57:07.153: ERROR/AndroidRuntime(287): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 03-07 15:57:07.153: ERROR/AndroidRuntime(287): at dalvik.system.NativeStart.main(Native Method)
你错过了一个空格:db.execSQL("delete * from " + TABLE_NAME);
db.execSQL("delete * from " + TABLE_NAME);
甚至不需要 include *,正确的查询是:
*
db.execSQL("delete from "+ TABLE_NAME);