6

I would like to know how to drop a database with ORMLite. Is there already any API call?

Just dropping all the tables does not delete the whole database.

Thanks in advance.

Lara
  • 61
  • 1
  • 2
  • You should probably answer and accept your own answer since you figured it out. I've copied your answer into mine if you just want to accept it. – Gray Sep 26 '12 at 20:33

2 Answers2

7

Edit:

Looks like you figured it out. You do something like:

boolean success =
    context.deleteDatabase(
        "/data/data/source.package.goes.here/databases/database-name.db‌​");

Edit:

Dropping a database is strange with ORMLite but I think it can be done. Really, when you do a dao.executeRaw(...) method, you have a connection open to the database engine that can perform just about any operation. You should be able to something like:

fooDao.executeRaw("drop database foo;");

That at least works for me under MySQL and it should under Sqlite.


Yes, ORMLite has the TableUtils class which allows you to create and drop tables. Here are the javadocs for the method.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • 1
    Thanks for your answer. Actually i am looking how to drop the database because dropping all the tables does not delete the whole database and this is what I actually want to do. Thanks. – Lara Aug 27 '12 at 12:37
  • I've updated my answer @Lara. Sorry for the confusion. Let me know if it does (or doesn't) work. – Gray Aug 27 '12 at 14:14
  • hey gray. thanks for the answer. i tried testDao.executeRaw("DROP DATABASE helloAndroid;", ""); in all combinations but somehow it always threw syntax-errors. i also tried to find the database on the phone itself, but somehow the path i could read out, does not exist on the phone magically. do you have any other ideas how to delete the database? thanks. Lara – Lara Aug 28 '12 at 11:47
  • 1
    Hrm. Maybe you can't do it via the Sqlite connector. See this answer: http://stackoverflow.com/questions/4406067/how-to-delete-sqlite-database-from-android-programmatically – Gray Aug 28 '12 at 13:17
  • I finally got the answer: boolean success = context.deleteDatabase("/data/data/package.for.testing/databases/helloAndroid.db"); – Lara Sep 13 '12 at 11:17
  • SQLite database FAQ: How do I drop a SQLite database? People used to working with other databases are used to having a "drop database" command, but in SQLite there is no similar command. The reason? In SQLite there is no "database server" -- SQLite is an embedded database, and your database is entirely contained in one file. So there is no need for a SQLite drop database command. To "drop" a SQLite database, all you have to do is delete the SQLite database file you were accessing. Copy from: http://alvinalexander.com/android/sqlite-drop-database-how – Than May 09 '14 at 11:16
  • If you are using deleteDatabase make sure all connection are closed to it, or else an error log will appear "file unlinked while open" and this can sometimes cause problems later on... – JanBo Feb 23 '16 at 09:26
-1

You could do something like this,

    TableUtils.dropTable(connectionSource, Model_Class.class, false);

for each table in the database, provided if you had model class for each table.

Reference:

http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/table/TableUtils.html#dropTable%28com.j256.ormlite.support.ConnectionSource,%20java.lang.Class,%20boolean%29