开发者

how to drop database in sqlite?

开发者 https://www.devze.com 2023-02-17 08:58 出处:网络
I\'m using SQLite in android. I want to dro开发者_运维问答p the database. For example: mysql- drop database dbname

I'm using SQLite in android. I want to dro开发者_运维问答p the database.

For example: mysql- drop database dbname

How do I implement this code in SQLite?


to delete your app database try this:

 this.deleteDatabase("databasename.db");

this will delete the database file


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


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.


try this :

 context.deleteDatabase(DATABASE_NAME);

How to delete SQLite database from Android programmatically


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.


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.


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


If you use SQLiteOpenHelper you can do this

        String myPath = DB_PATH + DB_NAME;
        SQLiteDatabase.deleteDatabase(new File(myPath));


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号