【发布时间】:2014-11-23 17:38:32
【问题描述】:
我有一个数据库版本 = 1 的应用。 现在我想将我的数据库升级到版本 = 2。
但是我的问题是,当我在先前版本上运行更新的应用程序并更改数据库版本时,它正在调用 onCreate() 方法而不是 onUpgrade()。
我知道onCreate()在第一次创建数据库时被调用,而onUpgrade()在我们更改数据库版本时被调用。
我遇到了很多堆栈溢出问题,但无法找到解决方案
注意:我有一个使用 SqliteManager 创建的预加载数据库,存储在资产文件夹中。 我到目前为止所做的如下
数据库:
private static String DB_PATH = "/data/data/context.getPackageName()/databases/";
private static String DB_NAME = "db_name";
private static final int DATABASE_VERSION = 2;
// reference of database
private SQLiteDatabase mySqliteDb;
private Context context;
public DBController(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
this.context = context;
}
/****************** onCreate() ***********************/
@Override
public void onCreate(SQLiteDatabase database) {
Log.v("On create method", "On create method"); // This log is printing
}
/********************* Upgrade ************************/
@Override
public void onUpgrade(SQLiteDatabase database, int version_old,
int current_version) {
Log.v("On upgrade method", version_old +"");
}
/************* Create Database *********************/
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
Log.v("DATABASE call ", "DATABASE exists"); // This log is printing
this.getWritableDatabase();
} else {
Log.v("DATABASE call ", "DATABASE not exist");
this.getWritableDatabase();
try {
copyDataBase();
} catch (IOException e) {
// Log.e("Error", e.toString());
throw new Error("Error copying database");
}
}
}
/***************** Check whether database exist or not *******************/
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = context.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[5120];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
/************************************ database open *************************************/
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
mySqliteDb = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
我在我的第一个活动中调用我的数据库
DBController dbController = new DBController(Login.this);
try {
dbController.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
在运行我的代码之后
Log.v("On create method", "On create method");
Log.v("DATABASE call ", "DATABASE exists");
这两个日志正在打印。
所以我无法升级我的数据库,因为它不会进入onUpgrade() 块。
如果我在这里遗漏了什么,请帮助我。
我发现了与堆栈溢出问题类似的问题。 Here 是链接。
【问题讨论】:
-
确保您设备上的 db 版本是 1(使用代码进行双重确认):D.
-
@DanielWilson 是的,我敢肯定,我也尝试了同样的方法,将其更改为版本 3
-
在 onUpgrade() 中调用 onCreate() - 也许你的 onUpgrade() 被删除了,因为它本质上是空的。可能没有,但值得一试。
-
@DanielWilson 我的
onUpgrade()实际上不是空的。我在那里插入一些数据。我只是认为没有必要将这些代码放在这里,因为onUpgrade()中的 Log 也没有打印:( -
很酷,是这样想的。这可能对您有帮助:stackoverflow.com/questions/14332462/… 我会将其添加为答案,因为我相信它会对其他人有所帮助
标签: android database sqlite android-sqlite sqliteopenhelper