【问题标题】:Android trouble opening existing SQLite database fileAndroid 无法打开现有的 SQLite 数据库文件
【发布时间】:2012-08-05 01:50:57
【问题描述】:

我有一个使用 Sqliteman 管理实用程序创建的 SQLite 数据库文件。我将创建的数据库文件复制到我的 Eclipse 项目的“资产”文件夹中(我应该导出到 .sql 文件吗?)。

因此,在代码中,我创建了自己的类,用于从现有文件路径打开和使用数据库。以下是相关代码:

    private static String DB_FULL_PATH_EXISTING = "/data/data/com.jds.databasetest/databases/Book1"; 

com.jds.databasetest 是我的包名,Book1 是 assets 文件夹中数据库文件的名称。现在在一些活动中:

    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() 打开东西吗?

标签: java android sql database sqlite


【解决方案1】:

我不确定,但也许您需要对 Data 的引用?

File data = Environment.getDataDirectory();

【讨论】:

  • 我不确定这会有什么帮助。我一直在关注他们只使用文件路径作为数据库字符串的示例:reigndesign.com/blog/…
  • 是的,我只记得在将数据库从 Data 备份到 SD 时,您需要该参考,抱歉。祝你好运。
猜你喜欢
  • 2014-11-11
  • 2014-08-30
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2015-08-03
  • 1970-01-01
相关资源
最近更新 更多