替换预先填充的数据库

时间:2016-02-15 23:52:50

标签: android sqlite

我创建了一个虚拟READONLY数据库,以确保我正确连接和应用程序读取。校验!有了这个工作,现在我通过sqlite管理器更新了数据库,但我读到覆盖现有的预先填充的数据库可能会造成严重破坏,所以我决定使用不同的名称导出新更新的版本。请注意,我使用与第一个相同的sqlite扩展名(它仍然可以)以相同的方式导出它但是这个会抛出以下加密/损坏的文件异常

  

android.database.sqlite.SQLiteDatabaseCorruptException:文件已加密或不是数据库(代码26)

我将新数据库复制并粘贴到assests文件夹中,然后继续注释掉旧的DB_NAME,然后用新的DB_NAME2替换代码的所有区域

我不明白为什么如果我连接它的方式与我之前的工作方式相同,这就不起作用

public class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_PATH;
private static final String DB_NAME2 = "DummyTestTwo.sqlite";
//    private static final String DB_NAME = "DummyTestOne.sqlite";
private SQLiteDatabase mDataBase;
private final Context mContext;

public static final String DBTABLENAME = "questiontable";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "question";
public static final String COLUMN_FID = "fid";

public DataBaseHelper(Context context) {
    super(context, DB_NAME2, null, 2);
    this.mContext = context;

    DB_PATH = context.getDatabasePath(DB_NAME2).getPath();


}

public void createDataBase() {
    boolean dbExist = checkDataBase();

    if (dbExist) {

    } else {
        this.getReadableDatabase();
    //            this.getWritableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;

    try {
        checkDB = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        Log.e(this.getClass().toString(), "Error while checking db");
    }

    if (checkDB != null) {
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {
    InputStream externalDbStream = mContext.getAssets().open(DB_NAME2);
    OutputStream localDbStream = new FileOutputStream(DB_PATH);

    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = externalDbStream.read(buffer)) > 0) {
        localDbStream.write(buffer, 0, bytesRead);
    }

    //Close the streams
    localDbStream.flush();
    localDbStream.close();
    externalDbStream.close();
}

public void openDataBase() throws SQLException {

    mDataBase = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close() {
    if (mDataBase != null) {
        mDataBase.close();
    }
    super.close();
}

public Cursor getCursorForAllQs() {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(DBTABLENAME);

    String[] asColumnsToReturn = new String[]{COLUMN_ID, COLUMN_NAME};

    Cursor mCursor = queryBuilder.query(mDataBase, asColumnsToReturn, null,
            null, null, null, "_id");

    return mCursor;
}

public String getName(Cursor c) {
    return (c.getString(1));
}

//    public String getFid(Cursor c) {
//        return (c.getString(2));
//    }

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

1 个答案:

答案 0 :(得分:0)

这是我找到的为我工作的解决方案。首先,我将两个db文件保存在我的assets文件夹中,并增加了数据库版本号。取消注释旧数据库,然后更改以下方法。

 public void openDataBase() throws SQLException {

    //mDataBase = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READONLY);
    mDataBase = this.getReadableDatabase();
}

另外,我将以下内容添加到 onUpgrade()

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion == 4) {
            Log.e("WORKED!!", "onUpgrade executed");
        }
        if (newVersion > oldVersion) {
            InputStream inputStream = null;
            OutputStream outputStream = null;
            String dbFilePath = DB_PATH + DB_NAME;

            try {
                inputStream = mContext.getAssets().open(DB_NAME2);

                outputStream = new FileOutputStream(dbFilePath);

                byte[] buffer = new byte[1024];
                int length;
                while ((length = inputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, length);
                }

                outputStream.flush();
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                throw new Error("Problem copying database from resource file.");
            }
        }
    }
相关问题