0
我有一個具有sqllite數據庫的報價應用程序。我已經發布了第一版的應用程序,它沒有任何數據庫版本的字段。現在我需要重新發布數據庫版本爲1的應用程序。我正在嘗試使用此代碼的自己,但獲得複製數據的問題。如果我想更新具有數據庫版本1的應用程序,我應該使用哪種條件,以便安裝沒有數據庫版本字段和新用戶的應用程序的舊用戶也不會遇到任何問題? 我的數據庫處理程序類如下所示。 感謝數據庫處理程序SQLlite版本問題
public class DataBaseHandler extends SQLiteOpenHelper {
private static String DB_PATH;
private static String DB_NAME = "SuccessQuotes";
private SQLiteDatabase myDataBase;
private final Context myContext;
private static final int DATABASE_VERSION = 1;
\t \t
\t \t public DataBaseHandler(Context context) {
\t super(context, DB_NAME, null, DATABASE_VERSION);
\t this.myContext = context;
\t DB_PATH = context.getDatabasePath(DB_NAME).toString();
\t Log.e("path", DB_PATH);
}
public void createDataBase() throws IOException {
\t boolean dbExist = checkDataBase();
\t if (dbExist) {
\t } else {
\t \t this.getReadableDatabase();
\t \t try {
\t \t \t copyDataBase();
\t \t } catch (IOException e) {
\t \t \t throw new Error("Error copying database");
\t \t }
\t }
}
// ==============================================================================
private boolean checkDataBase() {
\t SQLiteDatabase checkDB = null;
\t try {
\t \t String myPath = DB_PATH;
\t \t checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
\t } catch (SQLiteException e) {
\t \t // database does't exist yet.
\t }
\t if (checkDB != null) {
\t \t checkDB.close();
\t }
\t return checkDB != null ? true : false;
}
// ==============================================================================
private void copyDataBase() throws IOException {
\t // Open your local db as the input stream
\t InputStream myInput = myContext.getAssets().open(DB_NAME);
\t // Path to the just created empty db
\t String outFileName = DB_PATH;
\t // Open the empty db as the output stream
\t OutputStream myOutput = new FileOutputStream(outFileName);
\t // transfer bytes from the inputfile to the outputfile
\t byte[] buffer = new byte[1024];
\t int length;
\t while ((length = myInput.read(buffer)) > 0) {
\t \t myOutput.write(buffer, 0, length);
\t }
\t // Close the streams
\t myOutput.flush();
\t myOutput.close();
\t myInput.close();
}
// ==============================================================================
public void openDataBase() throws SQLException {
\t // Open the database
\t String myPath = DB_PATH;
\t myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
// ==============================================================================
@Override
public synchronized void close() {
\t if (myDataBase != null)
\t \t myDataBase.close();
\t super.close();
}
// ==============================================================================
@Override
public void onCreate(SQLiteDatabase db) {
}
// ==============================================================================
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
沒有得到你的問題.. – ELITE
如果你想採取以前版本的數據庫的副本,然後在SQLiteOpenHelper的'onUpgrade'方法中執行... – ELITE
我想更新沒有任何數據庫版本字段的應用程序。現在我想要數據庫版本= 1的更新應用程序,我應該在上面的代碼中更改哪些內容?我是sqllite數據庫和android中的新成員。 –