gpt4 book ai didi

java - 我的 onUpgrade() 方法应该是什么样子?

转载 作者:行者123 更新时间:2023-12-01 14:39:57 30 4
gpt4 key购买 nike

您可以在下面看到我的数据库助手类。我使用在 Assets 文件夹中导入的预填充 SQLite 数据库。每当我向现有数据库添加表时,如果我的应用程序已安装在手机上,则不会出现此类表错误。我想我的 onUpgrade() 方法现在已经很好了。它有效,不要误会我的意思,当我将一些数据更改为现有表时,我增加了数据库版本并且它得到更新。但如果我添加一个表,就会出现错误。

public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
//destination path (location) of our database on device
private static String DB_PATH = "/data/data/rs.androidaplikacije.themostcompleteiqtest/databases/";
private static String DB_NAME ="pitanja.sqlite";// Database name
private static SQLiteDatabase mDataBase;
private final Context mContext;
private static final int DATABASE_VERSION = 3;

public DataBaseHelper(Context mojContext)
{
super(mojContext, DB_NAME, null, 3);// 1 it's Database Version
DB_PATH = mojContext.getApplicationInfo().dataDir + "/databases/";
this.mContext = mojContext;
}

public void createDataBase() throws IOException
{
//If database not exists copy it from the assets


this.getReadableDatabase();
this.close();
try
{
//Copy the database from assests
copyDataBase();
Log.e(TAG, "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}

/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
public boolean checkDataBase(){

SQLiteDatabase checkDB = null;

try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}catch(SQLiteException e){

//database does't exist yet.

}

if(checkDB != null){

checkDB.close();

}

return checkDB != null ? true : false;
}
/*Check that the database exists here: /data/data/your package/databases/Da Name
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
*/

//Copy the database from assets
private void copyDataBase() throws IOException
{
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}

//Open the database, so we can query it
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DB_NAME;
//Log.v("mPath", mPath);
mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
//mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
return mDataBase != null;
}
@Override
public void close()
{
if(mDataBase != null)
mDataBase.close();
super.close();
}

@Override
public void onCreate(SQLiteDatabase arg0) {
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
try {
// delete existing?

// Copy the db from assests
copyDataBase();
Log.e(TAG, "database updated");
} catch (IOException mIOException) {
Log.e(TAG, mIOException.toString());
try {
throw mIOException;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

最佳答案

有 2 种方法可以执行 onUpgrade,具体取决于您的应用。

1)删除所有旧表,然后运行 ​​onCreate。这基本上会清除所有旧数据并重新开始。如果您可以以某种方式重新生成旧数据,或者只是不关心它,那么这是一种很好的技术。

2) 仔细维护每个发布版本之间架构的差异,并编写 SQL 语句以在它们之间进行适当的更改 - 添加新表、更改现有表以添加/删除列等。这是耗时且脆弱的,因此仅当您需要在这些版本之间保留数据时才使用此选项。

关于java - 我的 onUpgrade() 方法应该是什么样子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16069358/

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