2016-02-11 194 views
0

我創建一個使用Sqlite數據庫的應用程序。數據庫包含一個我正在使用sqlitebrowser進行更新的表。然而,每當我啓動數據庫時,應用程序中都看不到更改,可能是由於應用程序正在讀取舊數據庫。每次更新數據庫時,我都會更改DATABASE_VERSION,但它似乎沒有幫助。Sqlite數據庫不更新與應用程序更新

我相信這個問題是與我的onUpgrade()方法有關,但我不確定在它裏面放置什麼。

DBHelper類:

public class DBHelper extends SQLiteOpenHelper { 
    public static final String DATABASE_NAME = "BB2SoulDatabase.db"; 
    public String DB_PATH =""; 
    private static final int DATABASE_VERSION = 2; 
    public static final String TABLE_NAME = "SOULS"; 
    public static final String SOUL_COLUMN_NAME = "Name"; 
    private final Context myContext; 
    private SQLiteDatabase myDataBase; 

    public DBHelper(Context context) { 
     super(context, DATABASE_NAME , null, DATABASE_VERSION); 
     this.myContext = context; 
     DB_PATH = myContext.getDatabasePath(DATABASE_NAME).getPath(); 
    } 

    public void createDataBase() throws IOException { 
     boolean dbExist = checkDataBase(); 
     if(dbExist){ } 
     else { 
      this.getReadableDatabase(); 
      try { 
       copyDataBase(); 
      } catch (IOException e) { 
       throw new Error("Error copying database"); 
      } 
     } 
    } 
    private boolean checkDataBase(){ 
     SQLiteDatabase checkDB = null; 
     try { 
      String myPath = DB_PATH; 
      checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 
     } catch(SQLiteException e) { } 
     if(checkDB != null) { 
      checkDB.close(); 
     } 
     return checkDB != null ? true : false; 
    } 

    private void copyDataBase() throws IOException{ 
     InputStream myInput = myContext.getAssets().open(DATABASE_NAME); 
     String outFileName = DB_PATH; 
     OutputStream myOutput = new FileOutputStream(outFileName); 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = myInput.read(buffer))>0) { 
      myOutput.write(buffer, 0, length); 
     } 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 
    } 

    public void open() throws SQLException { 
     try { 
      createDataBase(); 
     } catch (IOException e) { 
      throw new Error("\n" + 
        "It was impossible to create the database"); 
     } 
     String myPath = DB_PATH; 
     myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 
    } 

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


    public Cursor getAllItems(String sortBy, String sortByAffinity, String sortByRace) { 
     SQLiteDatabase db = this.getReadableDatabase(); 
     String orderBy = " DESC"; 
     String affinity = ""; 
     String race = ""; 
     String raceAnd = " WHERE"; 
     if(sortByAffinity.equals("Quickstrike")) { 
      affinity = " WHERE Quickstrike = 'y'"; 
      raceAnd = " AND"; 
     } 
     else if(!sortByAffinity.equals("Affinity/All")){ 
      affinity = " WHERE Affinity = '"+sortByAffinity+ "'"; 
      raceAnd = " AND"; 
     } 

     if(!sortByRace.equals("Race/All")){ 
      race = raceAnd+" Race = '"+sortByRace+ "'"; 
     } 

     if(sortBy.equals("Name")) orderBy = " ASC"; 

     Cursor res = db.rawQuery("SELECT * FROM " + TABLE_NAME + affinity + race + " ORDER BY "+ sortBy + orderBy , null); 
     return res; 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
    } 

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

而不是使用'onCreate()'和'onUpdate()'從資產文件夾複製數據庫。在複製新數據庫之前,只需刪除舊數據庫。然後將值存儲在您的首選項中,以告訴您新的數據庫已被複制,因此不要一直複製它。 –

回答

2

您的代碼繞過SQLiteOpenHelper類的正常創建/升級框架,因此您必須自己處理升級。 (要麼刪除舊數據庫並複製新版本,要麼手動升級數據庫,具體取決於是否需要保留舊數據。)

使用類似SQLiteAssetHelper這樣的庫會更好:對你更容易。

如果您有一個只讀數據庫(即您知道不需要保留舊文件),只需更改文件名。這確保您可以簡單地複製新文件,如果它尚不存在。 (不要忘記刪除舊文件,如果它存在。)

1

場景:

你保持在外部目錄的數據庫是不是你的應用程序直接使用的數據庫。它在指定路徑中創建自己的數據庫,並從資產文件夾的數據庫中複製它的信息。

問題:

在你的代碼創建(&複印件)數據庫(一個應用程序直接使用),只有當它不存在。當你用sqlite瀏覽器創建新的數據庫時,你將它保存在外部目錄中。但該應用程序使用的數據庫已經存在。因此,它不會從外部目錄的新數據庫複製新數據。

解決方案:

卸載以前安裝的應用程序,然後安裝新的一個當你把變化的數據庫。

+0

當我更新應用程序不應該卸載以前的版本? @ZahanSafallwa – M0rty

+0

數據庫存儲在資產文件夾 – M0rty

+0

不知何故,它不會刪除android @ M0rty中的數據庫 –