Muneeb Mirza
Muneeb Mirza

Reputation: 920

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?

Upvotes: 6

Views: 6631

Answers (2)

Nidhish Krishnan
Nidhish Krishnan

Reputation: 20751

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

Upvotes: 3

laalto
laalto

Reputation: 152827

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.

Upvotes: 7

Related Questions