15

アセットフォルダーにある既存の sqlite データベースを使用する Android アプリケーションに取り組んでいます。だから私は実際にそのフォルダからデータベースをコピーします。ユーザーは自分のデータを保存することもできます (任意のコンテンツをお気に入りとしてマークします)。

バージョンを市場にアップロードしました。今、データベースにいくつかの新しいデータを追加し、新しいバージョンをアップロードしたいと考えています。ユーザーが市場からアプリを更新する場合、(以前のバージョンの) ユーザー データを保持し、新しいデータも追加します。私はいくつかのグーグルを行ったところ、アップグレード方法でトリックを行う必要があることがわかりました。しかし、コードから DATABASE_VERSION を変更していますが、アップグレード時のメソッドが呼び出されません。私は何かを逃したのだろうかと思っています。これが私のコードです:

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);


}

}

4

4 に答える 4

19

onUpgrade()データベースのバージョン番号の変更が検出された場合にのみ呼び出されます。次のようにデータベースのバージョンを増やします。

private static final int DATABASE_VERSION = 2;
于 2012-05-19T12:19:30.783 に答える
6

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");

                }
            }

        }

最後に、データベースのバージョンを上げて、もう一度実行してください。または同様の関数onUpgradeを呼び出すまで呼び出されません。getReadableDatabase()

お役に立てれば

于 2012-09-19T02:45:00.783 に答える
2

DATABASE_VERSION を変更したと仮定しても、それを実現するには onUpgrade にコードを追加する必要があります。魔法のようなものではありません。何をすべきかを伝える必要があります。

あなたの場合、次のことが必要です。

1) 古いデータベースをコピーします (新しい名前を付けます)
2) 新しいデータベースにコピーします
3) 古いデータベースからデータを読み込みます
4) 相違点を新しいデータベースにコピーします
5) 古いデータベースを削除します

編集

データベースを assets フォルダーに出荷すると仮定すると、次のようにコピーして使用できます。

@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(); 
}
于 2012-05-19T12:19:49.317 に答える
2

これを試してください (DATABASE_VERSION を増やします) & onUpdate() が呼び出されます:

private static final int DATABASE_VERSION = 2; 
于 2012-05-19T12:14:27.337 に答える