onUpgrade数据库 - oldVersion - 静态网页 [英] onUpgrade database - oldVersion - newVersion

查看:93
本文介绍了onUpgrade数据库 - oldVersion - 静态网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这DataBaseHelper.class和我被困在onUpgrade() - 方法。我不知道如何找出数据库的版本号。我可以设置版本为1时,我第一次发布它,当我发布一个更新我简直可以设置版本2 (myDataBase.setVersion(2)) 。但也只会是2,只要应用程序正在运行。下一次将启动再次为1。同样的情况,以私有静态诠释DATABASE_VERSION 。我在想存入额外的表的版本号,但这似乎并不在我看来,最好的做法。

那么,您如何确保版本号已经升级后增加的,它保持它(这是分配给私有静态诠释DATABASE_VERSION 或<$值C $ C> myDataBase.getVersion(); )

在DataBaseHelper级:

 公共类DataBaseHelper扩展SQLiteOpenHelper {

    //而Android的默认系统应用程序的数据库路径。
    私有静态字符串DB_PATH =/data/data/com.mydatabase.db/databases/;

    私有静态字符串DB_NAME =database.sl3;

    私人SQLiteDatabase MyDatabase的;

    私人最终语境myContext;


    //你需要这个?
    私有静态诠释DATABASE_VERSION = 2;
    //或者这是正确的:
    //私有静态诠释DATABASE_VERSION = myDataBase.getVersion();


    / **
     *构造函数
     *注意到并保持传递的上下文中的一个参考,以便获得
     *应用程序资产和资源。
     *
     * @参数方面
     * /
    公共DataBaseHelper(上下文的背景下){

        超(背景下,DB_NAME,空,DATABASE_VERSION);
        this.myContext =背景;
    }

    / **
     *在系统上创建一个空的数据库,并用自己的重写它
     * 数据库。
     * * /
    公共无效
        CreateDatabase用于()抛出IOException异常{
        布尔dbExist = checkDataBase();

        如果(dbExist){


        }
        其他 {

            //通过调用这个方法和空的数据库将被创建到系统​​默认路径
            //你的应用程序,所以我们要能够覆盖数据库,我们的数据库。


            this.getWritableDatabase();


            尝试 {

               copyDataBase();

            }赶上(IOException异常E){

                抛出新的错误(错误复制数据库);

            }
        }

    }

    / **
     *检查数据库中已存在,以避免重新复制每个文件
     *您打开应用程序。
     *
     * @返回true,如果它存在,虚假的,如果它不
     * /
    私人布尔
        checkDataBase(){

        SQLiteDatabase CHECKDB = NULL;

        尝试 {
            字符串mypath中= DB_PATH + DB_NAME;
            CHECKDB = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READWRITE);

        }赶上(SQLiteException E){

            //数据库的简化版,存在。

        }

        如果(CHECKDB!= NULL){

            checkDB.close();

        }
        返回CHECKDB!= NULL?真假;
    }

    / **
     *复制你的数据库从当地的资产文件夹复制到刚创建
     *在空数据库
     *系统文件夹,从那里它可以被访问和处理。
     *这是通过transfering字节流进行。
     * * /
    私人无效
        copyDataBase()抛出IOException异常{

        //打开本地数据库作为输入流
        InputStream的myInput = myContext.getAssets()开(DB_NAME)。


        //路径刚刚创建的空分贝
        字符串outFileName = DB_PATH + DB_NAME;


        //打开空分贝的输出流
        的OutputStream myOutput =新的FileOutputStream(outFileName);


        //传输的字节从inputfile中的OUTPUTFILE
        byte []的缓冲区=新的字节[1024];
        INT长;
        而((长度= myInput.read(缓冲液))大于0){
            myOutput.write(缓冲液,0,长度);
        }


        //关闭流
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    公共无效
       的openDatabase()抛出的SQLException {

        //打开数据库
        字符串mypath中= DB_PATH + DB_NAME;
        MyDatabase的= SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READWRITE);



        //我有哪些参数传递?
        onUpgrade(MyDatabase的,DATABASE_VERSION,2);
    }

    @覆盖
    市民同步无效
        关闭() {

        如果(MyDatabase的!= NULL)
            myDataBase.close();

        super.close();

    }

    @覆盖
    公共无效
        的onCreate(SQLiteDatabase DB){

    }

   @覆盖
   公共无效
        onUpgrade(SQLiteDatabase分贝,
                INT oldVersion,
                INT动态网页){

    Log.d(onUpgrade首次登录,Integer.toString(myDataBase.getVersion()));




    如果(oldVersion == 1){

        // 做一点事


        //然后做到这一点!?
        DATABASE_VERSION = 2;
        //或者这样做
        myDataBase.setVersion(2);
        Log.d(onUpgrade sedond日志,Integer.toString(myDataBase.getVersion()));

    }

    其他 {
        Log.d(onUpgrade,else从句:已升级);
    }



}
 

解决方案

  //你需要这个?
私有静态诠释DATABASE_VERSION = 2;
 

是的,你需要这个。 (甚至更好,使其最后了。)

这告诉数据库助手什么的数据库架构的最新版本。这应该是固定在你的应用程序code和递增只要你改变模式。

当你的应用程序启动时,辅助做了检查,在运行时的最新版本的code的想法是一样的这是积极的,当数据库上次创建或更新的版本。 (这就是 db.getVersion()是)。如果号码不匹配,那么助手知道存储的数据库外的日期相对于您的应用程序code,所以它运行升级程序。

这看起来好像你不是从头开始创建数据库,但是从你的资产导入现有的数据库。当你这样做的初始导入,这是时间要确保存储的版本相匹配的code的版本;或者直接将其应用到数据库文件中的资产,​​或者,如果你确定你的资产数据库文件相匹配的code,然后调用 setVersion(DATABASE_VERSION)

在任何情况下,你不应该试图修改版本号的 onUpgrade()程序。这只会被调用,如果版本不匹配,以及所有你应该做的,是让任何所需的更改,使数据库保持最新。该助手将管理的新版本号存储一旦升级完成。

I am using this DataBaseHelper.class and I am stuck on the onUpgrade()-method. I do not know how to figure out what the version number of the database is. I could set the version to 1, the first time i publish it and when I publish an update I simply could set the version to 2 (myDataBase.setVersion(2);). But it will only be 2 as long as the app is running. The next time it will be started it is 1 again. The same happens to private static int DATABASE_VERSION. I was thinking about storing the version number in an extra table but this does not seem to be best practice in my view.

So how do you make sure that the version number has increased after an upgrade and that it keeps it (the value which was assigned to private static int DATABASE_VERSION or myDataBase.getVersion();)?

The DataBaseHelper-class:

public class DataBaseHelper extends SQLiteOpenHelper {

    //The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/com.mydatabase.db/databases/";

    private static String DB_NAME = "database.sl3";

    private SQLiteDatabase myDataBase;

    private final Context myContext;


    // Do you need this?
    private static int DATABASE_VERSION = 2;
    // or is this correct:
    // private static int DATABASE_VERSION = myDataBase.getVersion();


    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to
     * the application assets and resources.
     * 
     * @param context
     */
    public DataBaseHelper(Context context) {

        super(context, DB_NAME, null, DATABASE_VERSION);
        this.myContext = context;
    }

    /**
     * Creates an empty database on the system and rewrites it with your own
     * database.
     * */
    public void
        createDataBase() throws IOException {
        boolean dbExist = checkDataBase();

        if (dbExist) {


        }
        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.getWritableDatabase();


            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 + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

        } catch (SQLiteException e) {

            //database does't exist yet.

        }

        if (checkDB != null) {

            checkDB.close();

        }
        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.
     * */
    private void
        copyDataBase() throws IOException {

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);


        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;


        //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();

    }

    public void 
       openDataBase() throws SQLException {

        //Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);



        // Which parameters do I have to pass?
        onUpgrade(myDataBase, DATABASE_VERSION, 2);
    }

    @Override
    public synchronized void
        close() {

        if (myDataBase != null)
            myDataBase.close();

        super.close(); 

    }

    @Override
    public void
        onCreate(SQLiteDatabase db) {

    }

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

    Log.d ("onUpgrade first log", Integer.toString(myDataBase.getVersion()));




    if (oldVersion == 1) {

        // do something


        // And then do this!?
        DATABASE_VERSION = 2;
        // or do this
        myDataBase.setVersion(2);
        Log.d ("onUpgrade sedond log", Integer.toString(myDataBase.getVersion()));

    }

    else {
        Log.d("onUpgrade", "else-clause: Already upgraded!");
    }



}

解决方案

// Do you need this?
private static int DATABASE_VERSION = 2;

Yes, you need this. (Even better, make it final too.)

This tells the database helper what the latest version of the database schema is. This should be fixed in your app code, and incremented whenever you alter the schema.

When your app starts up, the helper does a check at runtime that your code's idea of the latest version is the same as the version which was active when the database was last created or upgraded. (This is what db.getVersion() is for.) If the numbers don't match, then the helper knows that the stored database is out-of-date with respect to your application code, and so it runs the upgrade routine.

It looks as if you're not creating the database from scratch, but importing an existing database from your assets. When you do this initial import, this is the time at which to make sure the stored version matches your code's version; either apply it directly to the database file in your assets, or, if you're sure the database file in your assets matches the code, then you call setVersion(DATABASE_VERSION).

In any case, you shouldn't be trying to modify the version numbers in the onUpgrade() routine. This is only ever called if the versions don't match, and all you're supposed to do here is make whatever changes are needed to bring the database up-to-date. The helper will manage the storing of the new version number once the upgrade is complete.

这篇关于onUpgrade数据库 - oldVersion - 静态网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆