簡體   English   中英

SQLite數據庫未使用應用程序更新進行更新

[英]Sqlite database not updating with app update

我正在創建一個使用Sqlite數據庫的應用程序。 該數據庫包含一個表,我正在使用sqlitebrowser更新該表。 但是,每當我啟動數據庫時,更改都不會在應用程序中顯示,這可能是由於應用程序讀取了舊數據庫。 每次更新數據庫時,我都會更改DATABASE_VERSION ,但這似乎無濟於事。

我相信問題與我的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) {
    }
}

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

最好使用像SQLiteAssetHelper這樣的庫,它使您更輕松。

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

場景:

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

問題:

在您的代碼中,僅當一個數據庫不存在時才創建(並復制)該數據庫(一個應用程序直接使用)。 使用sqlite瀏覽器創建新數據庫時,會將其保留在外部目錄中。 但是應用程序使用的數據庫已經存在。 因此,它不會從外部目錄的新數據庫中復制新數據。

解:

卸載以前安裝的應用程序,然后在對數據庫進行更改時安裝新的應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM