• <bdo id='pi9aO'></bdo><ul id='pi9aO'></ul>

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

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

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

        <tfoot id='pi9aO'></tfoot>

        Android 为现有数据库升级数据库的简单方法是什么

        时间:2023-07-09
          <bdo id='72Y3z'></bdo><ul id='72Y3z'></ul>
        • <legend id='72Y3z'><style id='72Y3z'><dir id='72Y3z'><q id='72Y3z'></q></dir></style></legend>

              <tbody id='72Y3z'></tbody>

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

                <small id='72Y3z'></small><noframes id='72Y3z'>

                  本文介绍了Android 为现有数据库升级数据库的简单方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个应用程序,它有数据库,发布后.我已将一些表添加到我的数据库中.

                  I hava app, and it has database, after published. I have add some tables to the my database.

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

                  How can I easily update new database with old database which was already installed phone before when a customer update this application on market.

                  这是我的代码.onUpgrade应该写什么?

                  Here is my Code. What should I write in 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) {
                  
                          }}
                  

                  推荐答案

                  我已经用我的方法解决了!只需在 createDatabase 方法中添加一些代码.

                  Well I have solved with my way! just adding some codes in to createDatabase method.

                  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");
                          }
                          }
                      }
                  
                  }
                  

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

                  After all, I think best way is for upgrade is changing the database name..

                  这篇关于Android 为现有数据库升级数据库的简单方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:更新 SQLite 数据库 android 下一篇:如何在 android studio 中集成 sonarqube?

                  相关文章

                  1. <small id='T5Cjo'></small><noframes id='T5Cjo'>

                      <bdo id='T5Cjo'></bdo><ul id='T5Cjo'></ul>
                    <tfoot id='T5Cjo'></tfoot>
                  2. <legend id='T5Cjo'><style id='T5Cjo'><dir id='T5Cjo'><q id='T5Cjo'></q></dir></style></legend>

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