2014-09-20 48 views
0

我陷入了令人沮喪的境地。我有如下的SQLite數據庫類:即使在增加數據庫版本之後,SQLite數據庫也不會更新從Play Store更新應用程序

// The Android's default system path of your application database. 
private static String DB_PATH = "/data/data/com.example.ABC/databases/"; 

private static String DB_NAME = "ABCdb.sqlite"; 

private SQLiteDatabase myDataBase; 
private static int DATABASE_VERSION=2; 

private final Context myContext; 

/** 
* Constructor Takes and keeps a reference of the passed context in order to 
* access to the application assets and resources. 
* 
* @param context 
*/ 
public DBHelper(Context context) { 

    super(context, DB_NAME, null, DATABASE_VERSION); 
    this.myContext = context; 
} 

/** 
* Creates a empty database on the system and rewrites it with your own 
* database. 
* */ 
public void createDataBase() throws IOException { 

    boolean dbExist = checkDataBase(); 

    if (dbExist) { 
     // do nothing - database already exist 
     // By calling this method here onUpgrade will be called on a 
     // writable database, but only if the version number has been increased 

     this.getWritableDatabase(); 
    } else { 

     // By calling this method and empty database will be created into 
     // the default system path 
     // of your application so we are gonna be able to overwrite that 
     // database with our database. 
     this.getReadableDatabase(); 

     try { 

      copyDataBase(); 

     } catch (IOException e) { 
      System.out.println(e.toString()); 
      // throw new Error("Error copying database"); 

     } 
    } 

} 

/** 
* Check if the database already exist to avoid re-copying the file each 
* time you open the application. 
* 
* @return true if it exists, false if it doesn't 
*/ 
private boolean checkDataBase() { 

    SQLiteDatabase checkDB = null; 

    try { 
     String myPath = DB_PATH + DB_NAME; 
     checkDB = SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READWRITE); 

    } catch (SQLiteException e) { 

     // database does't exist yet. 

    } 

    if (checkDB != null) { 

     checkDB.close(); 

    } 

    return checkDB != null ? true : false; 
} 

/** 
* Copies your database from your local assets-folder to the just created 
* empty database in the system folder, from where it can be accessed and 
* handled. This is done by transfering bytestream. 
* */ 
private void copyDataBase() throws IOException { 

    // Open your local db as the input stream 
    InputStream myInput = myContext.getAssets().open(DB_NAME); 

    // Path to the just created empty db 
    String outFileName = DB_PATH + DB_NAME; 

    // Open the empty db as the output stream 
    OutputStream myOutput = new FileOutputStream(outFileName); 

    // transfer bytes from the inputfile to the outputfile 
    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 SQLiteDatabase openDataBase() throws SQLException { 

    // Open the database 
    String myPath = DB_PATH + DB_NAME; 
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, 
      SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
    return myDataBase; 
} 

@Override 
public synchronized void close() { 

    if (myDataBase != null) 
     myDataBase.close(); 

    super.close(); 

} 

@Override 
public void onCreate(SQLiteDatabase db) { 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    System.out.println("NewVersion : "+newVersion+", OldVersion : "+oldVersion); 
    if(newVersion>oldVersion){ 
     myContext.deleteDatabase(DB_NAME); 
     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

// Add your public helper methods to access and get content from the 
// database. 
// You could return cursors by doing "return myDataBase.query(....)" so it'd 
// be easy 
// to you to create adapters for your views. 

}

在Play商店中的應用程序上傳已DATABASE_VERSION設置爲1.I添加在DB.I一些新的表遞增DATABASE_VERSION爲2。但是當我使用調試密鑰庫運行應用程序時,onUpgrade()方法被調用並且數據庫正在升級,但是當我登錄應用程序並更新已安裝的應用程序時,它並未升級數據庫。我不知道我是否錯過了一些東西。任何幫助,高度讚賞。

回答

1

您應該只能用其他調試版本的應用升級應用的調試版本。發佈版本也是如此。發行版只能使用其他發行版進行升級。換句話說 - 如果你用一個已經用相同密鑰簽名的應用程序來替換應用程序,那隻能是升級。

因此,根據您的描述(在使用發行版時未能升級數據庫),聽起來好像您已將發行版替換爲調試版。 Android會將這種情況識別爲同一個應用程序,但它將首先卸載初始版本(調試),然後將其替換爲新版本(發行版)。因此,儘管用戶可能認爲這是升級,但這實際上是一種卸載/安裝場景。

據我所知,這是解釋你所看到的唯一方法。

+0

我已經使用了下面的場景來測試這個 – Orton 2014-09-20 06:37:38

+0

首先,我用發行密鑰存儲簽署了我的應用程序。然後,我增加了數據庫版本,並再次用發行密鑰庫簽名並安裝它。這次安裝嚮導告訴如果需要更新。我做了更新,數據庫升級了。但是當我在增加數據庫版本後上傳我的應用程序,然後從Play商店更新它。這次數據庫沒有升級。我知道這很奇怪,但不知道發生了什麼。 – Orton 2014-09-20 06:44:52