数据库onUpgrade方法不会被调用 [英] Database onUpgrade method is never called

查看:188
本文介绍了数据库onUpgrade方法不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从版本1升级我的数据库为2。我指定版本 DATABASE_VERSION = 2 (第一个版本被分配过),但 onUpgrade 从来没有所谓。 可能有一个在code明显的错误,但我看不出它东阳我试图解决这个问题,很长一段时间。

下面是数据库辅助类的code:

 公共类DatabaseHelper扩展SQLiteOpenHelper {

    私有静态最终诠释DATABASE_VERSION = 2;
    公共静态字符串DB_PATH;
    公共静态字符串DB_NAME =db.sqlite3;
    公共SQLiteDatabase数据库;
    公众最终上下文的背景下;

    公共DatabaseHelper(上下文的背景下){
        超(背景下,DB_NAME,空,DATABASE_VERSION);
        this.context =背景;

        文件直接=新的文件(Environment.getExternalStorageDirectory()
                +/.test);
        如果(!direct.exists()){
            如果(direct.mkdir())
                ;
        }
        DB_PATH = Environment.getExternalStorageDirectory()。getPath()
                +/.test/;
    }

    公共无效的CreateDatabase(){
        布尔dbExist = checkDataBase();
        如果(!dbExist){
            尝试 {
                copyDataBase();
            }赶上(IOException异常E){
                Log.e(this.getClass()的toString(),复制错误。);
                抛出新的错误(错误复制数据库!);
            }
        }
    }

    私人布尔checkDataBase(){
        SQLiteDatabase CHECKDB = NULL;
        尝试 {
            字符串路径= DB_PATH + DB_NAME;
            CHECKDB = SQLiteDatabase.openDatabase(路径,空,
                    SQLiteDatabase.OPEN_READONLY);
        }赶上(的SQLException E){
            Log.e(this.getClass()的toString(),同时检查数据库错误。);
        }
        如果(CHECKDB!= NULL){
            checkDb.close();
        }
        返回CHECKDB!= NULL;
    }

    私人无效copyDataBase()抛出IOException异常{
        InputStream的externalDbStream = context.getAssets()。打开(
                数据库/+ DB_NAME);
        字符串outFileName = DB_PATH + DB_NAME;
        的OutputStream localDbStream =新的FileOutputStream(outFileName);

        byte []的缓冲区=新的字节[1024];
        INT读取动作;
        而((读取动作= externalDbStream.read(缓冲液))大于0){
            localDbStream.write(缓冲液,0,读取动作);
        }
        localDbStream.close();
        externalDbStream.close();
    }

    公共SQLiteDatabase的openDatabase()抛出的SQLException {
        字符串路径= DB_PATH + DB_NAME;
        如果(数据库== NULL){
            的CreateDatabase();
            数据库= SQLiteDatabase.openDatabase(路径,空,
                    SQLiteDatabase.OPEN_READONLY);
        }
        返回数据库;
    }

    //一些不重要的方法

@覆盖
    公共无效的onCreate(SQLiteDatabase DB){
    }

    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        Log.w(myLogs,--- onUpgrade数据库---);
    }
 

在活动我称之为数据库是这样的:

  DatabaseHelper dbOpenHelper =新DatabaseHelper(本);
SQLiteDatabase数据库= dbOpenHelper.openDataBase();
 

在哪里是我的错? 帮助,请。

UPD: database.getVersion()返回0


更新的openDatabase()方法(它的工作):

 公共SQLiteDatabase的openDatabase()抛出的SQLException {
    字符串路径= DB_PATH + DB_NAME;
    如果(数据库== NULL){
        的CreateDatabase();
        数据库= SQLiteDatabase.openDatabase(路径,空,
            SQLiteDatabase.OPEN_READWRITE);
        this.getWritableDatabase();
    }
    返回数据库;
}
 

解决方案

本地数据库版本将始终为0和 onUpgrade 将不会被调用,因为你永远不叫 getWritableDatabase / getReadableDatabase ,这将凹凸版,返回本地数据库。

所以,你需要:

  1. 请电话 getReadableDatabase(); 在构造器/ OnCreate中,只是触发的过程中,如果数据库版本改变
  2. 在手动检查SD卡的版本, db.getVersion(); 比较当前版本 - DATABASE_VERSION 。而做任何你需要做的 - 升级或降级

I'm trying to upgrade my database from version 1 to version 2. I'm assigning DATABASE_VERSION = 2 (first version was assigned too) but onUpgrade never called. May be there is an obvious mistake in the code, but I can't see it beacause I'm trying to solve this problem for a long time.

Here is the code of database helper class:

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;
    public static String DB_PATH;
    public static String DB_NAME = "db.sqlite3";
    public SQLiteDatabase database;
    public final Context context;

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
        this.context = context;

        File direct = new File(Environment.getExternalStorageDirectory()
                + "/.test");
        if (!direct.exists()) {
            if (direct.mkdir())
                ;
        }
        DB_PATH = Environment.getExternalStorageDirectory().getPath()
                + "/.test/";
    }

    public void createDataBase() {
        boolean dbExist = checkDataBase();
        if (!dbExist) {
            try {
                copyDataBase();
            } catch (IOException e) {
                Log.e(this.getClass().toString(), "Copying error");
                throw new Error("Error copying database!");
            }
        }
    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDb = null;
        try {
            String path = DB_PATH + DB_NAME;
            checkDb = SQLiteDatabase.openDatabase(path, null,
                    SQLiteDatabase.OPEN_READONLY);
        } catch (SQLException e) {
            Log.e(this.getClass().toString(), "Error while checking db");
        }
        if (checkDb != null) {
            checkDb.close();
        }
        return checkDb != null;
    }

    private void copyDataBase() throws IOException {
        InputStream externalDbStream = context.getAssets().open(
                "databases/" + DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream localDbStream = new FileOutputStream(outFileName);

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

    public SQLiteDatabase openDataBase() throws SQLException {
        String path = DB_PATH + DB_NAME;
        if (database == null) {
            createDataBase();
            database = SQLiteDatabase.openDatabase(path, null,
                    SQLiteDatabase.OPEN_READONLY);
        }
        return database;
    }

    // some unimportant methods

@Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("myLogs", " --- onUpgrade database --- ");
    }

In Activity I call database like this:

DatabaseHelper dbOpenHelper = new DatabaseHelper(this);
SQLiteDatabase database = dbOpenHelper.openDataBase();

Where is my mistake? Help, please.

UPD: database.getVersion() returns 0


Updated openDataBase() method (it's working):

public SQLiteDatabase openDataBase() throws SQLException {
    String path = DB_PATH + DB_NAME;
    if (database == null) {
        createDataBase();
        database = SQLiteDatabase.openDatabase(path, null,
            SQLiteDatabase.OPEN_READWRITE);
        this.getWritableDatabase();
    }
    return database;
}

解决方案

Local database version will always be 0 and onUpgrade will not be called, because you never called getWritableDatabase/getReadableDatabase, which will bump version and return local db.

So you need:

  1. make call getReadableDatabase(); in your contructor/oncreate, just to trigger process, in case db version is changed
    or
  2. manually check sdcard version, db.getVersion(); compare to current version - DATABASE_VERSION. And do what ever you need to do - upgrade or downgrade.

这篇关于数据库onUpgrade方法不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆