Home » Android » How to delete all records from table in sqlite with Android?

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

Posted by: admin March 10, 2020 Leave a comment

Questions:

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)
How to&Answers:

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

Also there is no need to even include *, the correct query is:

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

Answer:

db.delete(TABLE_NAME, null, null);

or, if you want the function to return the count of deleted rows,

db.delete(TABLE_NAME, "1", null);

From the documentation of SQLiteDatabase delete method:

To remove all rows and get a count pass “1” as the whereClause.

Answer:

To delete all the rows within the table you can use:

db.delete(TABLE_NAME, null, null);

Answer:

SQLite doesn’t support the TRUNCATE command. You should use what you’ve tried in the previous line:

DELETE FROM `TABLE_NAME`;

P.S. You can optimize your program by using the same instance of the database connection for all of your queries to the given database instead of creating a new one for every query.

Answer:

There’s no need to use “execute” function.The following code worked for me:::

    db.delete(TABLE_NAME,null,null);
    db.close();

Answer:

SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DELETE FROM tablename"); //delete all rows in a table
db.close();

this work for me 🙂

Answer:

try this code to delete all data from a table..

String selectQuery = "DELETE FROM table_name ";
Cursor cursor = data1.getReadableDatabase().rawQuery(selectQuery, null);

Answer:

use Sqlit delete function with last two null parameters.

db.delete(TABLE_NAME,null,null)

Answer:

I use this class to handle database.I hope it will help some one in future.

Happy coding.

}

Usage:

Answer:

Just Write

or

Answer:

Answer:

Answer:

This is working for me. The difference is here with execSQL and rawQuery. The rawQuery use most in searching case and execSQL mostly used in apply operations.

Answer:

Can be usefull.

Answer:

this metod delate all data from database

Answer:

I have a function that works for me in Unity (in conjunction with the database SQLite).

code C#:

Answer:

Here is simple way to delete:

Here whereClause is optional, passing null will delete all rows in table. delete function will return number of affected row if whereClause passed otherwise will return 0.

Important Note: If you want to remove all rows and require count of deleted ones also then pass 1 as whereClause.