Java 未调用Sqlite数据库onUpgrade()
我正在开发一个android应用程序,其中我使用了资产文件夹中现有的sqlite数据库。所以我实际上是从那个文件夹复制数据库。用户还可以保存自己的数据(将任何内容标记为收藏夹) 我在市场上上传了一个版本。现在我想在数据库中添加一些新数据并上传一个新版本。如果用户从市场上更新应用程序,我希望保留用户数据(从以前的版本)并添加新数据。我在谷歌上做了一些研究,发现升级方法应该可以做到这一点。但是我正在从代码更改数据库_版本,但是升级时的方法没有得到调用。我想知道我错过了什么。这是我的密码:Java 未调用Sqlite数据库onUpgrade(),java,android,sqlite,upgrade,Java,Android,Sqlite,Upgrade,我正在开发一个android应用程序,其中我使用了资产文件夹中现有的sqlite数据库。所以我实际上是从那个文件夹复制数据库。用户还可以保存自己的数据(将任何内容标记为收藏夹) 我在市场上上传了一个版本。现在我想在数据库中添加一些新数据并上传一个新版本。如果用户从市场上更新应用程序,我希望保留用户数据(从以前的版本)并添加新数据。我在谷歌上做了一些研究,发现升级方法应该可以做到这一点。但是我正在从代码更改数据库_版本,但是升级时的方法没有得到调用。我想知道我错过了什么。这是我的密码: publi
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);
}
}尝试此操作(增加数据库版本)&将调用onUpdate()
private static final int DATABASE_VERSION = 2;
只有在检测到数据库版本号发生更改时,才会调用onUpgrade()。按如下方式增加数据库版本:
private static final int DATABASE_VERSION = 2;
假设您确实更改了数据库的版本,您仍然需要将代码放入onUpgrade中才能实现。这不是魔法,你必须告诉它该怎么做 在您的情况下,您需要: 1) 复制旧数据库(给它一个新名称)
2) 复制到新数据库中
3) 从旧数据库读取数据
4) 将任何差异复制到新数据库中
5) 删除旧数据库
编辑 假设您在assets文件夹中发送DB,您将复制它以供使用,如下所示:
@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();
}
将您的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()
或类似函数之前,不会调用code>onUpgrade
希望这有帮助我正在从代码更改数据库版本..,这到底意味着什么?我看到您使用一个常量字段
DATABASE\u VERSION
(显然无法更改),调用了SQLiteOpenHelper
的超级构造函数,那么您如何更改数据库版本呢?在应用程序的新版本中,我已将DATABASE\u VERSION更改为2。因此,在安装新版本时,应该调用onUpgrade方法。或者我遗漏了什么?首先,您发布的showa DATABASE_VERSION的代码是1,所以不会调用onUpgrade。第二,即使是2,您的onUpgrade也没有代码来更新数据库。这不是自动发生的,你必须这样做。我面临着同样的问题,你能解决这个问题吗?有人能帮我吗?我也面临着升级资产数据库的困难。Folder不会对他有多大好处,因为onUpgrade所做的就是吐出一条日志消息(至少带有发布的代码),OP问“为什么不调用onUpgrade
方法。“当然,它只会弹出一条日志消息,因为他还没有实现onUpgrade
@AlexLockwood->我已经将数据库版本更改为2,正如您所说,但logcat输出中没有显示任何内容。我已经用sqlite浏览器创建了数据库。我应该在创建数据库时进行版本控制吗?onUpgrade
仅在应用程序检测到数据库版本号发生更改时才会调用。因此,简单地增加版本号将导致调用一次onUpgrade
,并且在再次更改版本号之前不会再次调用它(即通过将数字增加到3)。当调用getReadableDatabase()或GetWriteableDatabase时,onUpgrade方法getcalled@Barak->我已将log.d语句放在onUpgrade()中测试。但是我在logcat输出中没有看到任何东西。这意味着onUpgrade不会被调用。在openDataBase()方法中,我放置了一个log.d语句。我可以看到数据库版本始终为1,即使我将数据库版本更改为2。对不起,我不明白你的意思。在我的例子中,如何复制新数据库。你可以发布一些代码吗我该怎么做…….对不起我的愚蠢问题…@Barak你能帮我吗我的onUpgrade方法没有被调用我已经按照你的回答了,但我的onUpgrade方法仍然没有被调用你能帮我吗