不调用Sqlite数据库onUpgrade()

时间:2012-05-19 11:51:18

标签: java android sqlite upgrade

我正在开发一个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);


}

}

4 个答案:

答案 0 :(得分:18)

仅当检测到数据库版本号的更改时,才会调用

onUpgrade()。按如下方式增加数据库版本:

private static final int DATABASE_VERSION = 2;

答案 1 :(得分:6)

将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");

                }
            }

        }

最后增加数据库版本并再次运行。在致电onUpgrade或类似功能之前,getReadableDatabase()不会被呼叫。

希望这有帮助

答案 2 :(得分:2)

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

private static final int DATABASE_VERSION = 2; 

答案 3 :(得分:2)

假设您确实更改了DATABASE_VERSION,您仍然需要在onUpgrade中放置代码才能实现。这不是神奇的,你必须告诉它该怎么做。

在您的情况下,您需要:

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

修改

假设您在资产文件夹中运送数据库,您可以复制它以供使用:

@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(); 
}