2013-12-20 53 views
2

這是我的數據庫幫助類:SQLite數據庫不會得到更新,除非我手動卸載,然後安裝新的APK

package tabswipe; 


import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import android.content.Context; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class ExternalDbOpenHelper extends SQLiteOpenHelper { 

    public static String DB_PATH; 
    public static String DB_NAME; 
    public SQLiteDatabase database; 
    public final Context context; 

    public SQLiteDatabase getDb() { 
     return database; 
    } 

    public ExternalDbOpenHelper(Context context, String databaseName) { 
     super(context, databaseName, null, 93); 
     this.context = context; 
     String packageName = context.getPackageName(); 
     DB_PATH = String.format("//data//data//%s//databases//", packageName); 
     DB_NAME = databaseName; 
     openDataBase(); 
    } 

    public void createDataBase() { 
     boolean dbExist = checkDataBase(); 
     if (!dbExist) { 
      this.getReadableDatabase(); 
      try { 
       copyDataBase(); 
      } catch (IOException e) { 
       Log.e(this.getClass().toString(), "Copying error"); 
       throw new Error("Error copying database!"); 
      } 
     } else { 
      Log.i(this.getClass().toString(), "Database already exists"); 
     } 
    } 
    private boolean checkDataBase() { 
     SQLiteDatabase checkDb = null; 
     try { 
      String path = DB_PATH + DB_NAME; 
      checkDb = SQLiteDatabase.openDatabase(path, null,SQLiteDatabase.OPEN_READONLY); 
     } catch (SQLException e) { 
      Log.e(this.getClass().toString(), "Error while checking db"); 
     } 
     if (checkDb != null) { 
      checkDb.close(); 
     } 
     return checkDb != null; 
    } 

    private void copyDataBase() throws IOException { 

     InputStream externalDbStream = context.getAssets().open(DB_NAME); 


     String outFileName = DB_PATH + DB_NAME; 


     OutputStream localDbStream = new FileOutputStream(outFileName); 


     byte[] buffer = new byte[1024]; 
     int bytesRead; 
     while ((bytesRead = externalDbStream.read(buffer)) > 0) { 
      localDbStream.write(buffer, 0, bytesRead); 
     } 

     localDbStream.close(); 
     externalDbStream.close(); 

    } 

    public SQLiteDatabase openDataBase() throws SQLException { 
     String path = DB_PATH + DB_NAME; 
     if (database == null) { 
      createDataBase(); 
      database = SQLiteDatabase.openDatabase(path, null,SQLiteDatabase.OPEN_READWRITE); 
     } 
     return database; 
    } 
    @Override 
    public synchronized void close() { 
     if (database != null) { 
      database.close(); 
     } 
     super.close(); 
    } 


    @Override 
    public void onCreate(SQLiteDatabase db) { 

     //createDataBase(); 
     openDataBase(); 

    } 



    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // on upgrade drop older tables 
     db.execSQL("DROP TABLE IF EXISTS perception_database"); 
     // create new tables 
     onCreate(db); 

    } 
} 

我不知道以什麼我應該在OnCreate和onUpgrade功能裏面把這麼以便在沒有首先清除數據和卸載的情況下便於更新數據庫。

我使用的是預填充的SQLite數據庫

數據庫名稱是umangdb.db

它包含了許多表,其中之一是perception_database而我試圖刪除並重新創建。

由於數據庫非常小,我不介意刪除和重新創建所有表。 我無法完全理解我從朋友那裏獲得的這個數據庫輔助類。

+1

請考慮使用'SQLiteAssetHelper'包裝數據庫作爲一種資產,並在網站上進行部署:https://github.com/jgilfelt/android-sqlite-asset-helper – CommonsWare

回答

3

SQLiteOpenHelper的構造函數的參數之一是DB的當前版本:see here。每次部署需要數據庫升級的應用程序的新版本時,都會增加版本,從而使其調用OnUpgrade方法,以便處理對數據庫所做的更改。

0

在onUpgrade功能,通過更高的版本號

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} 

您可以刪除故事並重新創建新表,如果你不需要測試過程中保存舊錶信息。

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // on upgrade drop older tables 
     db.execSQL("DROP TABLE IF EXISTS " + tablename); 

     // create new tables 
     onCreate(db); 
    } 

編輯: 你可以在OnCreate函數創建於ExternalDbOpenHelper類

public void onDelete(Context context){ 
    context.deleteDatabase(DATABASE_NAME); 
} 

下面的函數,調用它要破壞這個

db = new ExternalDbOpenHelper(getApplicationContext()); 
    db.onDelete(getApplicationContext()); 

它我的理解並在測試過程中重新創建數據庫。

+0

何種操作應該怎麼在onCreate函數中執行?請詳細回答。 –

相關問題