Sandip Armal Patil Sandip Armal Patil - 2 months ago 9
Android Question

How to delete all record from table in sqlite with Android?

My app has two buttons, the first button is for deleting record on user input and the second button is for deleting all records. But when I want to delete data it shows the message

"Your application has been forcefully stopped".

Please check my code and give me some suggestion.

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();
}


and

public void delete(String id)
{
String[] args={id};
getWritableDatabase().delete("texts", "_ID=?", args);
}


But it shows the following Log cat error.

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)

Answer

You missed a space: db.execSQL("delete * from "+ TABLE_NAME);

If this will give any error near *, you may consider removing * like this :

db.execSQL("delete from "+ TABLE_NAME);
Comments