2013-03-10 94 views
2

我有一个SQLite数据库从资产文件夹中的文件生成... 现在我知道这是不可能修改应用程序的资源, 但如果我释放更新应用程序,我想的是,已经创建的数据库将被清除,从新资源文件DB包含在应用程序的新版本重新创建从资产文件夹创建的SQLite数据库更新问题

这是我的数据库Helper类

public class DataBaseHelper extends SQLiteOpenHelper { 
    private static String TAG = "DataBaseHelper"; // Tag just for the LogCat 
                // window 
    // destination path (location) of our database on device 
    private static String DB_PATH = ""; 
    private static String DB_NAME = "";// Database name 
    private SQLiteDatabase mDataBase; 
    private final Context mContext; 

    public DataBaseHelper(Context context, String nomeDB) { 
     super(context, nomeDB, null, 2);// 2 is the Database Version 
     DB_NAME = nomeDB; 
     DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 
     this.mContext = context; 
    } 

    public void createDataBase() throws IOException { 
     // If database not exists copy it from the assets 

     boolean mDataBaseExist = checkDataBase(); 
     if (!mDataBaseExist) { 
      this.getReadableDatabase(); 
      this.close(); 
      try { 
       // Copy the db from assests 
       copyDataBase(); 
       Log.e(TAG, "createDatabase database created"); 
      } catch (IOException mIOException) { 
       Log.e(TAG, mIOException.toString()); 
       throw mIOException; 
      } 
     } 
    } 

    private boolean checkDataBase() { 
     File dbFile = new File(DB_PATH + DB_NAME); 
     // Log.v("dbFile", dbFile + " "+ dbFile.exists()); 
     return dbFile.exists(); 
    } 

    // Copy DB from assests 
    private void copyDataBase() throws IOException { 
     InputStream mInput = mContext.getAssets().open(DB_NAME); 
     String outFileName = DB_PATH + DB_NAME; 
     OutputStream mOutput = new FileOutputStream(outFileName); 
     byte[] mBuffer = new byte[1024]; 
     int mLength; 
     while ((mLength = mInput.read(mBuffer)) > 0) { 
      mOutput.write(mBuffer, 0, mLength); 
     } 
     mOutput.flush(); 
     mOutput.close(); 
     mInput.close(); 
    } 

    public boolean openDataBase() throws SQLException { 
     String mPath = DB_PATH + DB_NAME; 
     mDataBase = SQLiteDatabase.openDatabase(mPath, null, 
       SQLiteDatabase.CREATE_IF_NECESSARY); 
     return mDataBase != null; 
    } 

    @Override 
    public synchronized void close() { 
     if (mDataBase != null) 
      mDataBase.close(); 
     super.close(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 

    } 

} 

线

super(context, nomeDB, null, 2);// 2 is the Database Version 

应确定版本的广告,如果已经存在DB的版本号以前必须更新,

,所以如果我要解除与DB版本的新应用3

super(context, nomeDB, null, 3); 

应用程序必须从包含在应用程序的新编译版本中的资产重新创建数据库。

但我不知道正确的方法来做到这一点。

+0

你需要保存的东西旧的数据库? – wtsang02 2013-03-10 22:31:17

+0

@ wtsang02 不,我只需要清除数据库并从新的资产文件重新创建 – AndreaF 2013-03-10 22:33:29

回答

2

您需要使用onCreate()和onUpdate()的内置生命周期方法。

事情是这样的:

public class DataBaseHelper extends SQLiteOpenHelper { 
    private static String TAG = "DataBaseHelper"; // Tag just for the LogCat 
                // window 
    // destination path (location) of our database on device 
    private static final int DATABASE_VERSION = 3; 
    private static String DB_PATH = ""; 
    private static String DB_NAME = "";// Database name 
    private SQLiteDatabase mDataBase; 
    private final Context mContext; 

    public DataBaseHelper(Context context, String nomeDB) { 
     super(context, nomeDB, null, DATABASE_VERSION); 
     DB_NAME = nomeDB; 
     DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 
     this.mContext = context; 
    } 

    // Copy DB from assests 
    private void copyDataBase() throws IOException { 
     InputStream mInput = mContext.getAssets().open(DB_NAME); 
     String outFileName = DB_PATH + DB_NAME; 
     OutputStream mOutput = new FileOutputStream(outFileName); 
     byte[] mBuffer = new byte[1024]; 
     int mLength; 
     while ((mLength = mInput.read(mBuffer)) > 0) { 
      mOutput.write(mBuffer, 0, mLength); 
     } 
     mOutput.flush(); 
     mOutput.close(); 
     mInput.close(); 
    } 

    public boolean openDataBase() throws SQLException { 
     String mPath = DB_PATH + DB_NAME; 
     mDataBase = SQLiteDatabase.openDatabase(mPath, null, 
       SQLiteDatabase.CREATE_IF_NECESSARY); 
     return mDataBase != null; 
    } 

    @Override 
    public synchronized void close() { 
     if (mDataBase != null) 
      mDataBase.close(); 
     super.close(); 
    } 

    /** 
    * This method is called when the app doesn't have a database and a new one needs to be created 
    */ 
    @Override 
    public void onCreate(SQLiteDatabase arg0) { 
     try { 
      // Copy the db from assests 
      copyDataBase(); 
      Log.e(TAG, "database created"); 
     } catch (IOException mIOException) { 
      Log.e(TAG, mIOException.toString()); 
      throw mIOException; 
     } 
    } 

    /** 
    * This method is called when the app's database version is < the value passed into the constructor 
    */ 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     try { 
      // delete existing? 

      // Copy the db from assests 
      copyDataBase(); 
      Log.e(TAG, "database updated"); 
     } catch (IOException mIOException) { 
      Log.e(TAG, mIOException.toString()); 
      throw mIOException; 
     } 
    } 

}