2012-09-27 4 views
0

내 응용 프로그램에서 내 데이터베이스의 모든 것이 발생하는 dbhelper 클래스가 있는데, onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)을 사용해야하지만 전체 응용 프로그램을 업데이트하지 않으면 어떻게 호출해야하는지 이해하는 데 문제가 있습니다. 인터넷 호출에서 내 웹 테이블의 버전을 변경하여 내 응용 프로그램에서 테이블을 삭제하고 다시 작성하는 것이 좋습니다. 그래서 그들은 새로운 정보를 얻을 수 있습니다. 내 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에서 : DB에

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을해야하고 매개 변수로이 버전을 통과 :이 라인

변화

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

내가 완전히 제거해야합니다 (즉, SCHEMA_VERSION 제거주의)에

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

SCHEMA_VERSION을 쓰거나 이것을 씁니까? 개인 정적 int SCHEMA_VERSION = 1; – zvzej

+0

그래서이 변경 사항으로 웹에서 찾은 int 버전의 주 활동에서 전달할 것이라고 가정합니다. 이것이 작동하는 경우 DB 헬퍼 클래스는 해당 버전이 다른 경우 자체 검사합니다 이전 데이터베이스에 어떤 것이 저장되었고 onUpgrade를 다른 것으로 호출하는지 또는 onUpgrade를 주 활동에서 호출해야하는지? – zvzej

+0

예 자동으로 확인하고 onUpgrade를 호출합니다. 그리고 네, 분명히 호출자의 생성자,이 경우 귀하의 활동에이 매개 변수를 전달해야합니다. 그리고 다른 하나는 yes : SCHEMA_VERSION 필드를 제거하십시오. –

관련 문제