0

データベースを作成するための私のコードはこれです...

      public class DatabaseHandler extends SQLiteOpenHelper {



// ====================== String Variables ======================
// The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.example/databases/";
private static final String DATABASE_NAME = "test.sqlite"; // Database Name

// ++++++++++++++++++++++ int Variables ++++++++++++++++++++++
private static final int DATABASE_VERSION = 1; // Database Version



// = = = = = = = = = = = Other Variables = = = = = = = = = = ==
private SQLiteDatabase myDataBase;
private final Context myContext;
SQLiteDatabase db;

@SuppressWarnings("unused")
private String privateDirectoryPath;



public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.myContext = context;
    //  db = this.getWritableDatabase();

    //this.privateDirectoryPath = privateDirectory;
}
public void db_open()
{
    db = this.getWritableDatabase();
}
/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 * */
public void createDataBase() throws IOException {
    boolean dbExist = databaseExist();

    if (dbExist) {
        // do nothing - database already exist
    } else {
        // By calling this method and empty database will be created into
        // the default system path
        // of your application so we are gone a be able to overwrite that
        // database with our database.
        this.getReadableDatabase();
        try {
            copyDataBase(); // call method to copy database
            db = this.getWritableDatabase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

/**
 * Mrthod to check database is exist or not
 * @return
 */
public boolean databaseExist()
{
    File dbFile = new File(DB_PATH + DATABASE_NAME);
    return dbFile.exists();
}

/**
 * Copies your database from your local assets-folder to the just created
 * empty database in the system folder, from where it can be accessed and
 * handled. This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException {
    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DATABASE_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DATABASE_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[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

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

}



/**
 * Close Database
 */
@Override
public synchronized void close() {



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

public void DB_close()
{
    if(db!=null)
        db.close();
}



@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}



}

アセットからデータベースの新しい構造を変更すると...アプリでどのように更新されるか..古いデータを失うことなく.....
適切な指導をしてください...よろしくお願いします....

4

3 に答える 3

1
DbHelper mDHelper = new DbHelper(context, DB_NAME, null, DB_VERSION)
please pass version when dbhelper is created.
于 2013-09-13T05:21:40.897 に答える
0

データベースをアップグレードするには、クラスのonUpgradeメソッドをオーバーライドする必要があります。SQLiteOpenHelper

onUpgrade は基本的に、アプリの新しいバージョンの新しいデータベースの変更 (新しい列の追加、テーブルの追加など) を処理するためのものです。

于 2013-09-13T05:12:05.407 に答える
0

onUpgradeメソッドでデータベースの構造を変更する必要があります。assetsフォルダにありません。参照してください検索します。

于 2013-09-13T05:13:51.390 に答える