<small id='OSWUp'></small><noframes id='OSWUp'>

    <bdo id='OSWUp'></bdo><ul id='OSWUp'></ul>
  • <tfoot id='OSWUp'></tfoot>

    1. <i id='OSWUp'><tr id='OSWUp'><dt id='OSWUp'><q id='OSWUp'><span id='OSWUp'><b id='OSWUp'><form id='OSWUp'><ins id='OSWUp'></ins><ul id='OSWUp'></ul><sub id='OSWUp'></sub></form><legend id='OSWUp'></legend><bdo id='OSWUp'><pre id='OSWUp'><center id='OSWUp'></center></pre></bdo></b><th id='OSWUp'></th></span></q></dt></tr></i><div id='OSWUp'><tfoot id='OSWUp'></tfoot><dl id='OSWUp'><fieldset id='OSWUp'></fieldset></dl></div>
      1. <legend id='OSWUp'><style id='OSWUp'><dir id='OSWUp'><q id='OSWUp'></q></dir></style></legend>
      2. Android:onUpgrade 不在数据库升级时调用

        时间:2023-07-09

            <tbody id='HznPh'></tbody>

          <small id='HznPh'></small><noframes id='HznPh'>

          <tfoot id='HznPh'></tfoot>

                  <bdo id='HznPh'></bdo><ul id='HznPh'></ul>
                  <i id='HznPh'><tr id='HznPh'><dt id='HznPh'><q id='HznPh'><span id='HznPh'><b id='HznPh'><form id='HznPh'><ins id='HznPh'></ins><ul id='HznPh'></ul><sub id='HznPh'></sub></form><legend id='HznPh'></legend><bdo id='HznPh'><pre id='HznPh'><center id='HznPh'></center></pre></bdo></b><th id='HznPh'></th></span></q></dt></tr></i><div id='HznPh'><tfoot id='HznPh'></tfoot><dl id='HznPh'><fieldset id='HznPh'></fieldset></dl></div>
                  <legend id='HznPh'><style id='HznPh'><dir id='HznPh'><q id='HznPh'></q></dir></style></legend>
                • 本文介绍了Android:onUpgrade 不在数据库升级时调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发我的应用程序的第二个版本.我遇到了一个问题.

                  I am developing the second version of my application. In which I am facing a problem.

                  在我的应用程序中,数据库位于资产文件夹中.现在我想从第二个版本的 assets 文件夹中更新我的数据库.

                  In my application the database is in the assets folder. Now I want to update my database from the assets folder in the second version.

                  我尝试将代码升级为:

                  // Data Base Version.
                  private static final int DATABASE_VERSION = 2;
                  

                  我将版本从 1 更改为 2.

                  I changed the version from 1 to 2.

                  //构造函数

                  public DataBaseHelperClass(Context myContext) {     
                      super(myContext, DATABASE_NAME, null ,DATABASE_VERSION);
                      this.context = myContext;
                      //The Android's default system path of your application database.
                      DB_PATH = "/data/data/com.example.myapp/databases/";
                  }
                  

                  //创建数据库

                  public void createDataBase() throws IOException{
                      //check if the database exists
                      boolean databaseExist = checkDataBase();
                  
                      if(databaseExist){
                          this.getWritableDatabase();
                      }else{
                          this.getReadableDatabase();
                          try{
                              copyDataBase();
                          } catch (IOException e){
                              throw new Error("Error copying database");
                          }
                      }// end if else dbExist
                  } // end createDataBase().
                  

                  //检查数据库

                  public boolean checkDataBase(){
                      File databaseFile = new File(DB_PATH + DATABASE_NAME);
                      return databaseFile.exists();        
                  }
                  

                  //从资产中复制数据库

                  // Copy database from assets

                  private void copyDataBase() throws IOException{ 
                      //Open your local db as the input stream
                      InputStream myInput = context.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 input file to the output file
                      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(); 
                  }
                  

                  //打开数据库

                  public void openDataBase() throws SQLException{      
                      //Open the database
                      String myPath = DB_PATH + DATABASE_NAME;
                      sqliteDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);  
                  }
                  

                  //关闭数据库

                  @Override
                  public synchronized void close() { 
                      if(sqliteDataBase != null)
                          sqliteDataBase.close(); 
                      super.close(); 
                  }
                  
                  @Override
                  public void onCreate(SQLiteDatabase db) {
                      // No need to write the create table query.
                      // As we are using Pre built data base.
                      // Which is ReadOnly.
                  }
                  

                  //更新数据库时.

                  @Override
                  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                      if(newVersion>oldVersion){
                          context.deleteDatabase(DATABASE_NAME);
                          try {
                              copyDataBase();
                          } catch (IOException e) {
                              e.printStackTrace();
                          }
                      } else {
                  
                      }
                  }
                  

                  但问题是,在设备中执行 apk 后数据库没有更新.现在我该怎么办.

                  But the issue is that, the database is not updated after executing the apk in the device. Now what should I do.

                  我的应用程序的第一个版本已上市.第二版不知道怎么更新数据库.

                  My applications first version is on the market. Don't know how to update database in the second version.

                  请您提出宝贵的建议指导我,我正在申请中,无法移动.

                  Please guide me with your valuable suggestions, I am in middle of my application and I am not able to move.

                  onUpgrade 未调用.当我尝试在 onUpgrade 的第一行打印日志但未打印时.所以问题是,onUpgrade 没有调用.

                  EDIT : onUpgrade not calling. As i tried to print log in 1st line in onUpgrade but it is not printed. So the issue is that, onUpgrade is not calling.

                  推荐答案

                  为了升级你的数据库,你需要通过操作我找到的模式来正确地做这两个问题的答案都是有用的......

                  In order to upgrade your database you need to do properly by manipulating the schema I found Both the answers for my questions were useful...

                  自动升级数据库版本

                  这篇关于Android:onUpgrade 不在数据库升级时调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQLiteConstraintException 不要进入 catch 下一篇:更新 SQLite 数据库 android

                  相关文章

                    <bdo id='NLM15'></bdo><ul id='NLM15'></ul>

                • <tfoot id='NLM15'></tfoot>

                      <i id='NLM15'><tr id='NLM15'><dt id='NLM15'><q id='NLM15'><span id='NLM15'><b id='NLM15'><form id='NLM15'><ins id='NLM15'></ins><ul id='NLM15'></ul><sub id='NLM15'></sub></form><legend id='NLM15'></legend><bdo id='NLM15'><pre id='NLM15'><center id='NLM15'></center></pre></bdo></b><th id='NLM15'></th></span></q></dt></tr></i><div id='NLM15'><tfoot id='NLM15'></tfoot><dl id='NLM15'><fieldset id='NLM15'></fieldset></dl></div>
                    1. <small id='NLM15'></small><noframes id='NLM15'>

                      <legend id='NLM15'><style id='NLM15'><dir id='NLM15'><q id='NLM15'></q></dir></style></legend>