2014-03-19 38 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

另外,检查编辑我 –

相关问题