2016-10-15 3 views
1

New! 저는 android 앱 개발자입니다. 나는 onupgrade() 메소드가 여기에서 작동하지 않도록 도움이 필요하다. 아무도 나를 도울 수 없어요.Sqlite 데이터베이스 onupgrade()가 데이터베이스를 업데이트하지 않습니다.

전체 자바 코드가 제공됩니다.

내 문제는 데이터베이스 버전 1을 2로 변경하고 다시 작성하여 실행하면 내 데이터가 오래되었다는 것입니다. 그리고 업데이트 된 데이터가 필요합니다. 누구든지 나를 도울 수 있습니까?

감사합니다. 당신은 데이터베이스 관련 기능이나 코드 변경 후 이전 응용 프로그램 버전이 너무 새 데이터베이스 업데이트 작업을 업데이트 할 수 있습니다 경우

public class DataHandler extends SQLiteOpenHelper { 

    // Logcat tag 
    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_NAME = "status"; 
    private static final String DB_PATH = "/data/data/com.gracy.learnstatus/databases/"; 
    // Table Names 
    private static final String STATUS_TABLE_NAME = "mystatusall"; 
    // Common column names 
    private static final String KEY_STATUS_ID = "_id"; 
    private static final String KEY_STATUS_TAG = "tag"; 
    private static final String KEY_STATUS_STATUS = "name"; 
    private static final String KEY_STATUS_FAV = "fav"; 
    // Table Create Statements 
    private static final String STATUS_TABLE_CREATE = "CREATE TABLE " 
      + STATUS_TABLE_NAME + "(" 
      + KEY_STATUS_ID + " INTEGER PRIMARY KEY, " 
      + KEY_STATUS_TAG + " TEXT," 
      + KEY_STATUS_STATUS + " TEXT, " 
      + KEY_STATUS_FAV + " INTEGER " 
      + ")"; 
    private static final String[] STATUS_COLUMNS = new String[]{ 
      KEY_STATUS_ID, KEY_STATUS_TAG 
      , KEY_STATUS_STATUS, KEY_STATUS_FAV 
    }; 
    private static final String TAG = DataHandler.class.getSimpleName(); 
    private Context context; 

    // constructors 
    public DataHandler(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     this.context = context; 
     try { 
      createDataBase(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 


    @Override 
    public void onCreate(SQLiteDatabase db) { 
     try { 
      db.execSQL(STATUS_TABLE_CREATE); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // Drop older table if existed 
     db.execSQL("DROP TABLE IF EXISTS " + STATUS_TABLE_NAME); 
     try { 
      createDataBase(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     // create new tables 
     onCreate(db); 
    } 

    // Create Check And Copy Database 
    // Creates a empty database on the system and rewrites it with your own database. 
    public void createDataBase() throws IOException { 

     boolean dbExist = checkDataBase(); 

     if (dbExist) { 
      Log.i(TAG, "database created"); 
      //do nothing - database already exist 
     } 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) { 
       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 + DATABASE_NAME; 
      checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

     } catch (SQLiteException e) { 
      //database does't exist yet. 
     } 
     if (checkDB != null) { 
      checkDB.close(); 
     } 
     return checkDB != null; 
//  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. 
    */ 
    public void copyDataBase() throws IOException { 

     //Open your local db as the input stream 
     InputStream myInput = context.getAssets().open(DATABASE_NAME); 
     // Path to the just created empty db 
     String outFileName = DB_PATH + DATABASE_NAME; 
     File file = new File(outFileName); 
     if (file.delete()) { 
      Log.e(TAG, "DB Deleted"); 
     } 

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


} 
+0

중단 점 또는 로그를 사용하여 onUpgrade 메소드가 호출 중인지 확인하십시오. – USKMobility

+0

'assets'의 데이터베이스를 복사하고 자신의 데이터베이스를 만드는 것 사이에 혼합되어 있습니다 ... –

+0

버전 번호를 변경할 때 onUpgrade가 실제로 호출되지 않는지 여부를 USKMobility가 말하면서. 예외 인 경우 onUpgarde가 롤백되어 문제가 될 수 있습니다. 개인적으로 나는 앱이 시작될 때마다 실행되는 사용자 정의 메서드 (onExpand)를 사용하는 대체 방법을 사용합니다. 기본적으로 실제 구조를 원하는 구조와 비교하고 그에 따라 테이블과 열을 추가합니다 (심지어 모든 테이블과 따라서 열은 App Data를 삭제 한 후 말합니다)). – MikeT

답변

0

문제

당신은 이전 응용 프로그램을 제거하고 프로젝트를 다시 실행해야합니다 ..입니다.

+0

새 버전으로 데이터베이스를 업그레이드해야합니다. –

관련 문제