2012-09-27 60 views
0

在我的應用我有一個dbhelper課堂,一切都在我的數據庫事,我知道我必須使用onUpgrade(SQLiteDatabase分貝,INT oldVersion,INT NEWVERSION)但我在理解問題時遇到了問題,如果我不更新完整的應用程序,我只需要在因特網調用中發現我已更改我的Web表中的版本以便在我的應用程序中刪除表並重建他們可以得到新的信息。 這裏是我的代碼:如何升級我的出庫更新我的應用程序的Android

在我的BD幫手:

public class CuestionarioHelper extends SQLiteOpenHelper { 

private static String DB_PATH = "/data/data/myapp.VerCuestionarioEspanol_copy/databases/"; 
private static final String DB_NAME="cuestionario.db"; 
private static final int SCHEMA_VERSION=1; 
private SQLiteDatabase myData; 
public CuestionarioHelper(Context context) { 
    super(context, DB_NAME, null, SCHEMA_VERSION); 
} 
@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE Version (_id INTEGER PRIMARY KEY AUTOINCREMENT, version TEXT);"); 
    db.execSQL("CREATE TABLE Cuestionario (_id INTEGER PRIMARY KEY AUTOINCREMENT, pregunta TEXT, respuesta TEXT);"); 
    } 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + "Version"); 
    db.execSQL("DROP TABLE IF EXISTS " + "Cuestionario"); 
    onCreate(db); 
} 

,並在我的mainActivity:

private CuestionarioHelper db = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    db = new CuestionarioHelper(this); 

// here in a class DownloadFileAsync extends AsyncTask<String, String, String> 
    I download the version of the table in the web and if is different than the one 
    already in the db then it calls the table complete information to replace the one 
    in the apps db. 

//Here I verify what table version I have so I can add to a non existing table, 
    do nothing if both versions are the same or replace the bd table if they are different. 

public void verficar(){ 

verify = db.getTableCount(); 
if(verify == 0){ 
    tvVersion.setText("Version nueva "+Version.get(0).version); 
    // download the table information and added to the db 
}else{ 
    if(versionEntrada.equals(db.getVersion())){ 
     tvVersion.setText("Version ya existe "+db.getVersion()); 
      //version is the same do nothing 
    }else{ 
     int oldVersion = Integer.parseInt(db.getVersion()); 
     int newVersion = Integer.parseInt(versionEntrada); 
     db.onUpgrade(db, oldVersion, newVersion);//here is where I don't know 
                //how to call it, this is not working! 
     // download the table information and added to the db 
     tvVersion.setText("Version actualizada "+Version.get(0).version); 
    } 
} 
} 

在db版本表是我存儲表的版本也可能是數據庫版本,它只有一列和一行,因爲它只是一個數字來比較我的網絡表版本。

回答

2

你應該讓你的SCHEMA_VERSION not final,並通過該版本作爲一個參數:

改變這些線路

private static final int SCHEMA_VERSION=1; 
public CuestionarioHelper(Context context) { 
    super(context, DB_NAME, null, SCHEMA_VERSION); 
} 

到(注意SCHEMA_VERSION被刪除)

public CuestionarioHelper(Context context, int version) { 
    super(context, DB_NAME, null, version); 
} 
+0

應該怎麼徹底刪除SCHEMA_VERSION還是像這樣寫的? private static int SCHEMA_VERSION = 1; – zvzej

+0

所以我假設這個變化我從主要活動傳遞從網上找到的int版本,如果這是它是如何工作的,db helper類自己檢查,如果該版本不同於以前的數據庫中存儲了什麼,如果不同或者我必須從主Activity調用onUpgrade,它自己調用onUpgrade? – zvzej

+0

是的,它會自動檢查並呼叫升級。是的,顯然你必須將這個參數傳遞給調用者的構造函數,在這種情況下你的活動。還有一個是:刪除SCHEMA_VERSION字段。 –

相關問題