33

I'm using SQLite in android. I want to drop the database.

For example: mysql- drop database dbname

How do I implement this code in SQLite?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Rajesh Sharma
  • 573
  • 1
  • 5
  • 15

9 Answers9

60

to delete your app database try this:

 this.deleteDatabase("databasename.db");

this will delete the database file

thenosic
  • 1,463
  • 2
  • 18
  • 22
  • 4
    you must have access to the context to do this. –  Jul 01 '12 at 12:40
  • +1 for the answer... I just wanted to downgrade the database which is not allowed so I had to delete the database... :) – AL̲̳I Sep 08 '14 at 15:00
  • I know this is a bit late, but it IS possible to downgrade your database. It's just disabled by default. You would need to implement it yourself. – Passer by Feb 19 '17 at 01:03
41

The concept of creating or dropping a database is not meaningful for an embedded database engine like SQLite. It only has meaning with a client-sever database system, such as used by MySQL or Postgres.

To create a new database, just do sqlite_open() or from the command line sqlite3 databasefilename.

To drop a database, delete the file.

Reference: sqlite - Unsupported SQL

ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235
  • An embedded db engine means for me a file based database as seen with sqlite, so deleting the db.db file will drop the db. – Timo Mar 22 '22 at 16:24
7

try this :

 context.deleteDatabase(DATABASE_NAME);

How to delete SQLite database from Android programmatically

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
7

You can drop tables by issuing an SQL Command as you would normally. If you want to drop the whole database you'll have to delete the file. You can delete the file located under

data/data/com.your.app.name/database/[databasefilename]

you can do this from the eclipse view called "FileBrowser" out of the "Android" Category for example. Or directly on your emulator or phone.

Chris
  • 7,675
  • 8
  • 51
  • 101
5
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

boiledwater
  • 10,372
  • 4
  • 37
  • 38
5

From http://www.sqlite.org/cvstrac/wiki?p=UnsupportedSql

To create a new database, just do sqlite_open(). To drop a database, delete the file.

stuartd
  • 70,509
  • 14
  • 132
  • 163
5

If you want to delete database programatically you can use deleteDatabase from Context class:

deleteDatabase(String name)
Delete an existing private SQLiteDatabase associated with this Context's application package.

Caner
  • 57,267
  • 35
  • 174
  • 180
2

If you use SQLiteOpenHelper you can do this

        String myPath = DB_PATH + DB_NAME;
        SQLiteDatabase.deleteDatabase(new File(myPath));
Gabriel
  • 3,564
  • 1
  • 27
  • 49
0

SQLite saves data to a file. Leverage the methods tailored to the specific OS so you can delete the file. I.E. with Android use the System.IO.File.Delete() method.

Here is some code showing what I used to create and delete my SQLite database on an android device in C#:

public class SQLite_DB 
{
    private string databasePath;

    public SQLiteAsyncConnection GetConnection()
    {
        databasePath = Path.Combine(FileSystem.AppDataDirectory, "sqlite_db.db3");
        SQLiteAsyncConnection database = new SQLiteAsyncConnection(databasePath);
        return database;
    }

    public bool DeleteDatabase()
    {
        File.Delete(databasePath);

        if (File.Exists(databasePath))
        return false;

        return true;
    }
}

You may also be able to use the Java.IO.File class.