我正在使用SQLite在安卓上。 我想删除数据库。
例如: mysql- drop database dbname
我该如何在SQLite中实现这段代码?
要删除你的应用程序数据库,请尝试以下步骤:
this.deleteDatabase("databasename.db");
这将删除数据库文件
对于像SQLite这样的嵌入式数据库引擎,创建或删除数据库的概念是没有意义的。它只在客户端服务器数据库系统中有意义,例如MySQL或Postgres。
要创建一个新数据库,只需使用sqlite_open()
函数或在命令行中使用sqlite3 databasefilename
命令。
要删除一个数据库,只需删除相应的文件即可。
表
。如果您想要删除整个数据库,您需要删除该文件。您可以删除位于
data/data/com.your.app.name/database/[databasefilename]
您可以从名为“FileBrowser”的Eclipse视图中执行此操作,例如“Android”类别。或直接在模拟器或手机上执行。
Context
类中的deleteDatabase
方法:
deleteDatabase(String name)
删除与此上下文应用程序包相关联的现有私有SQLiteDatabase。
从http://www.sqlite.org/cvstrac/wiki?p=UnsupportedSql
要创建一个新的数据库,只需使用sqlite_open()。要删除数据库,请删除文件。
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.
从http://alvinalexander.com/android/sqlite-drop-database-how复制
在Android中,如果您需要删除SQLite数据库,可以使用以下代码:
public void deleteDatabase(Context context) {
context.deleteDatabase("database_name");
}
这个方法会删除指定名称的数据库。请注意,如果数据库不存在,则不会发生任何事情。
String myPath = DB_PATH + DB_NAME;
SQLiteDatabase.deleteDatabase(new File(myPath));
SQLite将数据保存到文件中。利用特定操作系统的方法,您可以删除该文件。例如,在Android上使用System.IO.File.Delete()方法。
以下是一些代码示例,展示我在C#中创建和删除SQLite数据库时在Android设备上所使用的方法:
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;
}
}
您还可以使用 Java.IO.File 类。