2010-06-06 45 views
0

我已經發布了具有初始數據庫的應用程序(世界時間)。現在我想用數據庫升級來更新應用程序。初始安裝時數據庫版本爲零

我已經在OnUpgrade()中加入了升級代碼並檢查了newVersion。但它並沒有在我的本地測試中被調用...

所以我把調試語句來獲取數據庫版本,它是零。

任何想法爲什麼它沒有被版本化?

以下是將數據庫從我的資產文件夾中複製代碼...

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(); 

以下是我的構造,在OnCreate()& OnUpgrade()

public DatabaseHelper(Context context) { 

    super(context, DB_NAME, null, DB_VERSION); 
    this.myContext = context; 
} 
@Override 
public void onCreate(SQLiteDatabase db) { 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    if(oldVersion == 1 && newVersion == 2){ 
     String sql = "update statement...."; // i have a correct update statement here 
     db.execSQL(sql); 
    } 

} 

-
馬赫什
http://android.maheshdixit.com

+0

當你讀取檢查-1而不是0。 – 2010-06-06 15:25:41

+1

如果您希望獲得關於'onCreate()'和'onUpgrade()'的'SQLiteOpenHelper'實現方面的幫助,您需要顯示'onCreate()'和'onUpgrade()'''SQLiteOpenHelper'的實現。 – CommonsWare 2010-06-06 17:33:33

+0

@Override 公共無效的onCreate(SQLiteDatabase分貝){ \t} \t @Override \t公共無效onUpgrade(SQLiteDatabase分貝,INT oldVersion,INT NEWVERSION){ \t \t如果(oldVersion == 1 && NEWVERSION == 2){ \t \t \t String sql =「some sql statement ...」; \t \t \t db.execSQL(sql); \t \t} \t} 公共DatabaseHelper(上下文上下文){ \t超級(上下文,DB_NAME,NULL,DB_VERSION); this.myContext = context; } – mahesh 2010-06-06 17:40:10

回答

0

你應該hav e是您的數據庫適配器的類。這將有類似以下行:

private static final String DATABASE_NAME = "sr4.db"; 
private static final int DATABASE_VERSION = 1;

所有你需要做的就是應用程序識別數據庫需要更新的更改號碼DATABASE_VERSION設置爲。

+0

感謝您的回覆...但我已經有以下 private static final String DB_NAME =「WorldClock」; private static final int DB_VERSION = 2; public DatabaseHelper(Context context){ \t super(context,DB_NAME,null,DB_VERSION); this.myContext = context; } \t 我也嘗試明確設置版本爲1,但下一次升級它不會調用OnUpgrade() – mahesh 2010-06-06 16:38:55

1

您不應該手動檢查數據庫版本,如果正確完成,SQLite會爲您執行此操作。

在你SQLiteOpenHelper繼承類,在構造函數中,你應該通過DB_VERSION,像這樣:

public class DBHelper extends SQLiteOpenHelper { 
    public static final String DB_NAME = "your_db_name"; 
    public static final int DB_VERSION = 2; 

    public DBHelper(Context context) { 
     super(context, DB_NAME, null, DB_VERSION); 
       ... 
     } 
    ... 
} 

然後在您的onCreate()執行create table和onUpgrade()做任何你需要如果升級時沒有檢查版本,只有當SQLite已經爲你檢查了這個方法時纔會調用這個方法。如果你需要在onUpgrade()中運行代碼,只需增加你的DB_VERSION變量。

0

我發現了這個問題......看起來OnCreate()和OnUpgrade只有在調用getReadableDatabases或getWriteableDatabases()時纔會被調用。如果數據庫存在,我不會調用他們中的任何一個。我改爲調用SQLLiteDatabase.openDatabase(),它不調用OnCreate或OnUpgrade()。

現在我明確調用getWriteableDatabases(),它調用OnUpgrade()。

感謝您的迴應,雖然...