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 回答