我有一个使用Sqliteman管理实用程序创建的SQLite数据库文件。我将创建的数据库文件复制到Eclipse项目的“ Assets”文件夹中(我应该导出到.sql文件吗?)。
因此,在代码中,我创建了自己的类,用于打开和使用现有文件路径中的数据库。以下是相关代码:
private static String DB_FULL_PATH_EXISTING = "/data/data/com.jds.databasetest/databases/Book1";
其中com.jds.databasetest是我的程序包名称,Book1是资产文件夹中数据库文件的名称。现在进行一些活动:
SQSimple existingDB = new SQSimple(this, "Book1", DB_FULL_PATH_EXISTING);
existingDB.open();
SQSimple是我的自定义数据库类。以下是相关的构造函数代码:
public SQSimple(Context c, String DBname, String existingDBpath) {
this.mCtx = c;
this.DBname = DBname;
this.DBpath = existingDBpath;
this.fromExisting = true;
}
public SQSimple open() throws android.database.SQLException {
if (this.fromExisting == false) {
dbHelper = new DatabaseHelper(this);
db = dbHelper.getWritableDatabase();
return this;
} else { //opening from existing DB, i.e. this code gets run
db = SQLiteDatabase.openDatabase(this.DBpath, null, SQLiteDatabase.OPEN_READWRITE);
return this;
}
}
因此,仅尝试通过existingDB.open()打开数据库会导致应用程序崩溃。 Logcat表示以下内容:
E/Database(13144): sqlite3_open_v2("/data/data/com.jds.databasetest/databases/Book1", &handle, 2, NULL) failed
希望我正在做的事情定义明确,并且我在这里遗漏了一些显而易见的东西。
非常感谢您的帮助。
最佳答案
我不确定,但也许您需要引用数据?
File data = Environment.getDataDirectory();
关于java - Android无法打开现有的SQLite数据库文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11813374/