【问题标题】:Android What is the easist way Upgrade Database for existing DBAndroid 为现有数据库升级数据库的简单方法是什么
【发布时间】:2014-03-19 10:12:21
【问题描述】:

我有一个应用程序,它有数据库,发布后。 我已经在我的数据库中添加了一些表。

当客户在市场上更新此应用程序时,我如何可以轻松地使用已安装手机的旧数据库更新新数据库。

这是我的代码。 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) {

        }}

【问题讨论】:

  • 使用Database Class的OnUpdate()方法,

标签: android database sql-update


【解决方案1】:

如果您使用SqliteOpenHelper,则需要将数据库版本增加到新版本。然后您将使用数据库实例和新版本以及数据库正在更新的版本调用方法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);
}

【讨论】:

  • onUpgrade 应该写什么?
  • 你不应该按照你的方式打开数据库,你应该使用SQLiteOpenHelper中已经存在的实例。您应该调用 getWritableDatabase() 或 getReadableDatabase()。回到您的问题,如果您要通过添加、删除表或更改表来更新数据库,您的 onUpgrade 方法将在更改数据库版本后调用一次,并且在此方法中使用“db”实例,这是您应该使用的数据库实例添加您需要的任何修改
  • 我已经检查过了,但我的 newTableQuery 是什么?和 dropTableQuery?
  • 这些是数据库更新的示例。您应该使用这种方法为市场上的用户创建所有要创建的新表
【解决方案2】:

好吧,我已经用我的方式解决了!只需在 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");
        }
        }
    }

}

毕竟,我认为升级的最佳方法是更改​​数据库名称..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    相关资源
    最近更新 更多