【问题标题】:Sqlite database onUpgrade() does not get calledSqlite 数据库 onUpgrade() 没有被调用
【发布时间】:2012-05-26 18:06:16
【问题描述】:

我正在开发一个 android 应用程序,在该应用程序中我使用了资产文件夹中现有的 sqlite 数据库。所以我实际上从那个文件夹中复制了数据库。用户还可以保存自己的数据(将任何内容标记为收藏)。

我在市场上上传了一个版本。现在我想在数据库中添加一些新数据并上传一个新版本。如果用户从市场更新应用程序,我想保留用户数据(来自以前的版本)并添加新数据。我做了一些谷歌,发现升级方法应该可以解决问题。但是我正在从代码中更改 DATABASE_VERSION,但是 on upgrade 方法没有被调用。我想知道我错过了什么。这是我的代码:

public class DataBaseHelper extends SQLiteOpenHelper {

private static String DB_PATH = "/data/data/riskycoder.login/databases/";
public static String DB_NAME = "datenbank_EN.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
private static final int DATABASE_VERSION = 1;





public DataBaseHelper(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
    } else {
        this.getWritableDatabase();
        try {
            this.close();
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }

}

private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
    }
    if (checkDB != null)
        checkDB.close();
    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {

    InputStream myInput = myContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[2048];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();

    //myDataBase.setVersion(DATABASE_VERSION);
}

public void openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
    Log.d("Test", "Database version: " +myDataBase.getVersion());
}

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



@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.d("Test", "in onUpgrade. Old is: " + oldVersion + " New is: " + newVersion);


}

}

【问题讨论】:

  • 我正在从代码中更改 DATABASE_VERSION..,这到底是什么意思?我看到你用常量字段DATABASE_VERSION(显然不能更改)调用SQLiteOpenHelper的超级构造函数,那么你究竟如何更改数据库版本?
  • 在新版本的应用程序中,我已将 DATABASE_VERSION 更改为 2。因此,在安装新版本时,应调用 onUpgrade 方法。还是我错过了什么?
  • 首先,您发布的代码显示 DATABASE_VERSION 为 1,因此不会调用 onUpgrade。其次,即使它是 2,您的 onUpgrade 也没有代码可用于更新数据库。它不会自动发生,你必须这样做。
  • 我遇到了同样的问题,你能解决这个问题吗?
  • 谁能帮帮我,我也面临升级资产文件夹数据库的困难

标签: java android sqlite upgrade


【解决方案1】:

onUpgrade() 仅在检测到数据库版本号发生变化时调用。增加数据库版本如下:

private static final int DATABASE_VERSION = 2;

【讨论】:

  • 不会对他有多大好处,因为 onUpgrade 所做的只是吐出一条日志消息(至少带有发布的代码)
  • OP 询问“为什么不调用 onUpgrade 方法。”当然它只会吐出一条日志消息,因为他还没有实现onUpgrade
  • @AlexLockwood-> 如您所说,我已将 DATABASE_VERSION 更改为 2,但 logcat 输出中未显示任何内容。我已经通过 sqlite 浏览器创建了数据库。我应该在创建数据库时进行任何版本控制吗?
  • onUpgrade 仅在您的应用检测到DATABASE_VERSION 号码发生变化时调用。因此,简单地增加版本号将导致 onUpgrade 被调用一次,并且在版本号再次更改之前不会再次被调用(即通过将数字增加到 3)。
  • 当 getReadableDatabase() 或 getWritableDatabase 被调用时 onUpgrde 方法被调用
【解决方案2】:

将您的 onUpgrade 更改为以下

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

            if(newVersion > oldVersion){                    
                    myContext.deleteDatabase(DB_NAME);
            }
        }

并像下面一样更改您的 createDataBase(),

public void createDataBase() throws IOException{

            boolean dbExist = checkDataBase();

            if(dbExist){
                // By calling this method here onUpgrade will be called on a
                // writable database, but only if the version number has been increased

                this.getWritableDatabase();
            }

            dbExist = checkDataBase();

            if(!dbExist){

                //By calling this method an empty database will be created into the                     default system path
                   //of the application so we will be able to overwrite that database with our database.
                this.getReadableDatabase();

                try {

                    copyDataBase();

                } catch (IOException e) {

                    throw new Error("Error copying database");

                }
            }

        }

最后增加您的数据库版本并再次运行它。在您调用 getReadableDatabase() 或类似函数之前,onUpgrade 不会被调用。

希望对你有帮助

【讨论】:

  • 我已经按照你的回答,但我的 onUpgrade 方法仍然没有被调用,你能帮我吗
【解决方案3】:

试试这个(增加 DATABASE_VERSION)& onUpdate() 将被调用:

private static final int DATABASE_VERSION = 2; 

【讨论】:

  • @Barak-> 我已将 log.d 语句放在 onUpgrade() 中进行测试。但我在 logcat 输出中看不到任何内容。这意味着 onUpgrade 不会被调用。在 openDataBase() 方法中,我放置了一个 log.d 语句。即使我将 DATABASE_VERSION 更改为 2,我也可以看到数据库版本始终为 1。
【解决方案4】:

假设您确实更改了 DATABASE_VERSION,您仍然必须将代码放入 onUpgrade 中才能实现。这并不神奇,你必须告诉它该做什么。

在您的情况下,您需要:

1) 复制旧数据库(给它一个新名称)
2) 复制到你的新数据库
3) 从旧数据库中读取数据
4) 将任何差异复制到新数据库中
5) 删除旧数据库

编辑

假设您将数据库放在 assets 文件夹中,您可以像这样复制它以供使用:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // First copy old db
    InputStream bakInput = getActivity().getAssets().open(dbname);
    String bakFileName = getActivity().getAssets() + "YourDB.old";
    OutputStream bakOutput = new FileOutputStream(bakFileName);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = bakInput.read(buffer)) > 0) {
        bakOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();

    // delete the old db
    File delDB = new File(getActivity().getAssets() + "YourDB"); 
    boolean deleted = file.delete(); 

    // Copy in the new db
    InputStream myInput = getActivity().getAssets().open("YourDB");
    String outFileName = MAIN_DB_PATH + dbname;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();

    // add code here to get changes user has made to db and move them to updated db

    // delete the old db copy
    File delDB = new File(getActivity().getAssets() + "YourDB.old"); 
    boolean deleted = file.delete(); 
}

【讨论】:

  • 对不起,我不明白你。就我而言,我如何复制新数据库。你能发布一些代码我该怎么做......对不起我的愚蠢问题......
  • @Barak 你能帮我我的 onUpgrade 方法没有被调用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多