2016-10-15 13 views
1

私はアンドロイドアプリデベロッパーで新しい人物です。私はonupgrade()メソッドがここで動作しないようにする必要があります。誰でも私を喜ばせることができます。Sqliteデータベースonupgrade()はデータベースを更新していません

完全なJavaコードが提供されています。

私の問題は、データベースのバージョンを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)を使用して代替メソッドを使用します(実際の構造を必要な構造と比較し、それに応じてテーブルとカラムを追加します) )。 – MikeT

答えて

0

問題は、データベース関連の機能やコードの変更以前のアプリケーションのバージョンが新しいデータベース更新タスク..だから

を更新することはできません場合は、以前のアプリケーションをアンインストールし、プロジェクトを再実行する必要があります。

+0

新しいバージョンのデータベースをアップグレードする必要があります。 –

関連する問題