The TRUNCATE TABLE statement is used to remove all records from a table. SQLite does not have an explicit TRUNCATE TABLE command like other databases. Instead, it has added a TRUNCATE optimizer to the DELETE statement. To truncate a table in SQLite, you just need to execute a DELETE statement without a WHERE clause.
You can delete the files yourself, but if any process accessing the database has crashed, or some process still has the database open, you may lose changes.
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);
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.
To delete all the rows within the table you can use:
db.delete(TABLE_NAME, null, null);
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.
There's no need to use "execute" function.The following code worked for me:::
db.delete(TABLE_NAME,null,null);
db.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With