Android难以升级SQlite表而不会丢失旧数据

时间:2013-09-13 05:07:20

标签: android sqlite

我创建数据库的代码就是这个......

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

}



}

现在,当我从资产中更改数据库的新结构时...它将如何在应用程序中更新...而不会丢失旧数据.....
     请给我适当的giudence ...     提前谢谢....

3 个答案:

答案 0 :(得分:1)

DbHelper mDHelper = new DbHelper(context, DB_NAME, null, DB_VERSION)
please pass version when dbhelper is created.

答案 1 :(得分:0)

您必须覆盖onUpgrade类的SQLiteOpenHelper方法才能升级数据库。

onUpgrade基本上用于处理任何新版本应用的新数据库更改(可能是新列添加,表添加)。

答案 2 :(得分:0)

您必须使用onUpgrade方法更改数据库的结构。不在assets文件夹中。 RefferSearch