2014-03-19 77 views
-1

我hava應用程序,它有數據庫,發表後。 我有添加一些表到我的數據庫。Android什麼是easist方式現有數據庫升級數據庫

當客戶在市場上更新此應用程序之前,如何輕鬆更新已安裝電話的舊數據庫的新數據庫。

這是我的代碼。我應該在onUpgrade寫什麼?

public class DataBaseHelper extends SQLiteOpenHelper 
    { 

    //destination path (location) of our database on device 
    private static String DB_PATH = ""; 
    private static String DB_NAME ="sorubankasi.sqlite";// Database name 
    private SQLiteDatabase mDataBase; 
    private final Context mContext; 
    private static final int DATABASE_VERSION = 2;// new versiyon 
    private static final String tag = "stk"; 

    public DataBaseHelper(Context context) 
    { 
     super(context, DB_NAME, null, DATABASE_VERSION);// 1? its old Database Version 
     DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 
     this.mContext = context; 
    }  

    public void createDataBase() throws IOException 
    { 

     boolean mDataBaseExist = checkDataBase(); 
     if(!mDataBaseExist) 
     { 
      this.getReadableDatabase(); 
      this.close(); 
      try 
      { 
       copyDataBase(); 
      } 
      catch (IOException mIOException) 
      { 
       throw new Error("ErrorCopyingDataBase"); 
      } 
     } 
    } 


     //Check that the database exists here: /data/data/your package/databases/Da Name 
     private boolean checkDataBase() 
     { 
      File dbFile = new File(DB_PATH + DB_NAME); 
      //Log.v("dbFile", dbFile + " "+ dbFile.exists()); 
      return dbFile.exists(); 
     } 

     //Copy the database from assets 
     private void copyDataBase() throws IOException 
     { 
      InputStream mInput = mContext.getAssets().open(DB_NAME); 
      String outFileName = DB_PATH + DB_NAME; 
      OutputStream mOutput = new FileOutputStream(outFileName); 
      byte[] mBuffer = new byte[1024]; 
      int mLength; 
      while ((mLength = mInput.read(mBuffer))>0) 
      { 
       mOutput.write(mBuffer, 0, mLength); 
      } 
      mOutput.flush(); 
      mOutput.close(); 
      mInput.close(); 
     } 

     //Open the database, so we can query it 
     public boolean openDataBase() throws SQLException 
     { 
      String mPath = DB_PATH + DB_NAME; 
      //Log.v("mPath", mPath); 
      mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY); 
      //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); 

      Log.d(tag, "opendatabase " + mDataBase.getVersion()); 
      return mDataBase != null; 
     } 

     @Override 
     public synchronized void close() 
     { 
      if(mDataBase != null) 
       mDataBase.close(); 
      super.close(); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase arg0) { 

     } 

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

     }} 
+0

使用的OnUpdate數據庫類的()方法, – Kedarnath

回答

0

嗯,我有我的方式解決了!只需在createDatabase方法中添加一些代碼即可。

public void createDataBase() throws IOException { 

    boolean mDataBaseExist = checkDataBase(); 
    if (!mDataBaseExist) { 

     this.getReadableDatabase(); 
     this.close(); 
     try { 
      copyDataBase(); 
     } catch (IOException mIOException) { 
      throw new Error("ErrorCopyingDataBase"); 
     } 
     } 
    else{ 
     SQLiteDatabase db = getReadableDatabase(); 
     if(db.getVersion()<DATABASE_VERSION){ 
     this.getReadableDatabase(); 
     this.close(); 
     try { 
      copyDataBase(); 
     } catch (IOException mIOException) { 
      throw new Error("ErrorCopyingDataBase"); 
     } 
     } 
    } 

} 

畢竟,我認爲最好的辦法是升級更改數據庫名稱..

1

如果您使用SqliteOpenHelper你需要的數據庫版本增加新version.And那麼你會得到方法onUpgrade()調用數據庫實例和新版本的數據庫從更新的版本。

@Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    //create upgrades needed between the two versions "oldVersion" to "newVersion" 
    db.execSQL(newTableQuery); 
    db.execSQL(dropTableQuery); 
    } 

否則,如果您是直接使用開放的數據庫,沒有使用的SqliteOpenHelper,你將不得不通過檢查「yourDatabaseInstance.getVersion()」,如果數據庫的版本比特定版本較低檢查數據庫版本,使新的更新和那麼你的數據庫版本到新版本使用「yourDatabaseInstance.setVersion()

if(yourDatabaseInstance.getVersion() < newDatabaseVersion) 
{ 
     //create upgrades needed between the two versions "oldVersion" to "newVersion" 
     yourDatabaseInstance.setVersion(newDatabaseVersion); 
} 
+0

,我應該寫onUpgrade什麼? – mehmet

+0

您不應該像打開數據庫那樣打開數據庫,您應該使用SQLiteOpenHelper中的實例。你應該調用getWritableDatabase()或getReadableDatabase()。回到你的問題,如果您要通過添加,刪除表或更改表來更新數據庫,那麼在更改數據庫版本後,您的onUpgrade方法將被調用一次,並且在此方法中使用「db」實例,該實例是您的數據庫實例添加你需要的任何修改 –

+0

另外,檢查編輯我 –

相關問題