How to Truncate All Tables in Sqlite Database using Java?
I know i can do this
{
...
String table_name1 = "Delete From table_name1";
String table_name2 = "Delete From table_name2";
String table_name3 = "Delete From table_name3";
String table_name4 = "Delete From table_name4";
String table_name5 = "Delete From table_name5";
stmt.executeUpdate(table_name1);
...
}
I can execute all these Strings to do the task but it increases the Lines of Code.
Is there a way to Truncate all tables in a Database with a single command, without separately typing there names?
There's no TRUNCATE
in sqlite. You can use DELETE FROM <tablename>
to delete all table contents. You'll need to do that separately for each table.
However, if you're interested in removing all data, consider just deleting the database file and creating a new one.
SQLite
do not have TRUNCATE TABLE
command in SQLite
but you can use SQLite DELETE
command to delete complete data from an existing table, though it is recommended to use DROP TABLE
command to drop complete table and re-create it once again.
Try
{
...
String table_name1 = "DELETE FROM table_name1";
String table_name2 = "DELETE FROM table_name2";
String table_name3 = "DELETE FROM table_name3";
String table_name4 = "DELETE FROM table_name4";
String table_name5 = "DELETE FROM table_name5";
stmt.executeUpdate(table_name1);
...
}
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