我有一个SQLite数据库文件,使用Sqliteman管理工具创建。我将我创建的数据库文件复制到Eclipse项目的“Assets”文件夹中(我是否应该导出到.sql文件?)。Android的麻烦打开现有的SQLite数据库文件
所以在代码中,我创建了我自己的类,用于从现有文件路径中打开和使用数据库。下面是相关代码:
private static String DB_FULL_PATH_EXISTING = "/data/data/com.jds.databasetest/databases/Book1";
哪里com.jds.databasetest是我的包名,第一册是在资产文件夹中的数据库文件的名称。现在进行一些活动:
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
希望我所做的是非常明确的,我错过了一些相当明显的东西。
非常感谢您的帮助。
此外,我想补充一点:我不确定在我的情况下,dbHelper是如此常用的。我有文件路径...我不能用SQLiteDatabase.openDatabase()打开这个东西吗? – JDS 2012-08-05 02:11:12
http://stackoverflow.com/questions/9109438/how-to-use-existing-database-with-android-app/9109728#9109728 – 2012-08-06 05:03:54
这是否解决了? – 2012-11-18 23:45:01