簡體   English   中英

Android無法打開現有的SQLite數據庫文件

[英]Android trouble opening existing SQLite database file

我有一個使用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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM