gpt4 book ai didi

android - 是什么触发了 SqliteDbOpenHelper onUpgrade()?

转载 作者:太空宇宙 更新时间:2023-11-03 10:54:37 25 4
gpt4 key购买 nike

有人能解释一下如何在 SqliteDbOpenHelper 类中调用 onUpgrade 函数吗?我正在打开我的数据库,但它没有调用 onUpgrade,即使我已经更改了 DB_VERSION。

见以下代码:

public class DbHelper extends SQLiteOpenHelper {
private static final String TAG = "DbHelper";

static final String DB_NAME = "caddata.sqlite";
static final int DB_VERSION = 4;

private static String DB_PATH = "";
private Context myContext;
private SQLiteDatabase myDataBase;

public DbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;

DB_PATH = "/data/data/"
+ context.getApplicationContext().getPackageName()
+ "/databases/";
}

public DbHelper open() throws SQLException {
myDataBase = getWritableDatabase();

Log.d(TAG, "DbHelper Opening Version: " + this.myDataBase.getVersion());
return this;
}

@Override
public synchronized void close() {

if (myDataBase != null)
myDataBase.close();

super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "onCreate called");

try {
createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if ( newVersion > oldVersion)
{
Log.d(TAG, "New database version exists for upgrade.");
try {
Log.d(TAG, "Copying database...");
copyDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void createDataBase() throws IOException {

boolean dbExist = checkDataBase();

if (!dbExist) {

try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}

openDataBaseForRead();
}


private boolean checkDataBase() {

SQLiteDatabase checkDB = null;

try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY
| SQLiteDatabase.NO_LOCALIZED_COLLATORS);
Log.d(TAG, "db exists");
} catch (SQLiteException e) {
// database does't exist yet.
Log.d(TAG, "db doesn't exist");

}

if (checkDB != null) {
checkDB.close();
}

return checkDB != null ? true : false;
}


private void copyDataBase() throws IOException {

// Open your local db as the input stream
InputStream myInput = myContext.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[2048];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}

// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();

myDataBase.setVersion(DB_VERSION);
}

public void openDataBaseForRead() throws SQLException {

// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}

public void openDataBaseForWrite() throws SQLException {

// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.NO_LOCALIZED_COLLATORS );
}


}

另请参阅此处详细说明我的问题:Why is onUpgrade() not being invoked on Android sqlite database?我没有得到回复。

我的主要 Activity 从助手打开数据库。我假设只要打开数据库就会调用 onUpgrade() 函数:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

DbHelper myDbHelper = new DbHelper(this);

try {

myDbHelper.createDataBase();

} catch (IOException ioe) {

throw new Error("Unable to create database");

}

try {

myDbHelper.openDataBaseForRead();

}catch(SQLException sqle){

throw sqle;

}
}

最佳答案

在我看来,onCreate()onUpgrade() 函数仅在您使用 SQLiteOpenHelper 打开数据库时起作用 。这意味着你应该调用函数getWritableDatabase()getReadbleDatabase()来打开数据库,然后它也许可以正常工作。

关于android - 是什么触发了 SqliteDbOpenHelper onUpgrade()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7661376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com