Programmatically erase data of a sqlite database using ormlite library

P. Sohm picture P. Sohm · Feb 16, 2012 · Viewed 11.2k times · Source

I'd looking for a method to erase all data of a ormlite database or delete the database (and then recreate it) with ormlite on android.

At this time, I can only change the DATABASE_VERSION of the DatabaseHelper.

But I have to compile the application.

Does someone know a method to handle that case?

Answer

Gray picture Gray · Feb 16, 2012

I'd looking for a method to erase all data of a ormlite database or delete the database (and then recreate it) with ormlite on android.

@Julia's answer will work well. ORMLite also supports a TableUtils.clearTable() method call which removes all rows from a table:

That won't clear a database but you can clear each table in turn. Something like the following:

TableUtils.clearTable(getConnectionSource(), YourClassHere.class);

Edit:

@max4ever pointed out that context.deleteDatabase(...) is a lot faster than other ways of clearing a database. But this call will remove the table definitions while TableUtils.clearTable(...) leaves the schema intact.