Sqlite数据库onUpgrade()不会被调用

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

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

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

}

Solutions Collecting From Web of "Sqlite数据库onUpgrade()不会被调用"

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

 private static final int DATABASE_VERSION = 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才会被调用。

希望这可以帮助

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

 private static final int DATABASE_VERSION = 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(); }