Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java-Sqlite Truncate all Database tables

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?

like image 605
Muneeb Mirza Avatar asked Mar 24 '14 13:03

Muneeb Mirza


2 Answers

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.

like image 112
laalto Avatar answered Nov 10 '22 15:11

laalto


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);
   ...
}
like image 39
Nidhish Krishnan Avatar answered Nov 10 '22 14:11

Nidhish Krishnan