升级数据库 [英] upgrading database

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

问题描述

我已经在谷歌store.I一个应用程序正在使用具有3个表,并在第一次启动应用程序的复制它内置的数据库。现在,我想要做升级应用程序,并添加另一个表。下面是我的code。

 公共DataBaseHelper(上下文的背景下)
{
    超级(上下文,DB_NAME,空,1);
    DB_PATH =/数据/数据​​/+ context.getPackageName()+/数据库/;
    this.mContext =背景;
}

公共无效的CreateDatabase()抛出IOException异常
{
    //如果数据库不存在从资产复制

    布尔mDataBaseExist = checkDataBase();
    如果(!mDataBaseExist)
    {
        this.getReadableDatabase();
        this.close();
        尝试
        {
            //复制从资产产生数据库
            copyDataBase();
            Log.e(TAG的CreateDatabase数据库中创建);
        }
        赶上(IOException异常mIOException)
        {
            抛出新的错误(ErrorCopyingDataBase);
        }
    }
}
    //检查数据库存在的位置:/数据/数据​​/软件包/数据库/ DA名称
    私人布尔checkDataBase()
    {
        文件DBFILE =新的文件(DB_PATH + DB_NAME);
        返回dbFile.exists();
    }

    //复制从资产数据库
    私人无效copyDataBase()抛出IOException异常
    {
        InputStream的mInput = mContext.getAssets()开(DB_NAME)。
        字符串outFileName = DB_PATH + DB_NAME;
        的OutputStream mOutput =新的FileOutputStream(outFileName);
        byte []的mBuffer =新的字节[1024];
        INT mLength;
        而((mLength = mInput.read(mBuffer))大于0)
        {
            mOutput.write(mBuffer,0,mLength);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }

    //打开数据库,这样我们就可以查询它
    公共布尔的openDatabase()抛出的SQLException
    {
        字符串的mpath = DB_PATH + DB_NAME;
        //Log.v("mPath的mpath);
        mDataBase = SQLiteDatabase.openDatabase(的mpath,空,SQLiteDatabase.CREATE_IF_NECESSARY);
        // mDataBase = SQLiteDatabase.openDatabase(的mpath,空,SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        返回mDataBase!= NULL;
    }

    @覆盖
    市民同步无效关闭()
    {
        如果(mDataBase!= NULL)
            mDataBase.close();
        super.close();
    }

    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)
    {
        Log.w(TAG,从版本升级数据库+ oldVersion +到
                +动态网页+,这将销毁所有旧数据);
        字符串SQL =CREATE TABLE IF NOT EXISTS CKRecordings(+
                _id INTEGER PRIMARY KEY AUTOINCREMENT,+
                Name文本,文件路径文本,creationDate文本);
        db.execSQL(SQL);
        db.execSQL(DROP TABLE IF EXISTS数据);
        的onCreate(DB);
    }

    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        // TODO自动生成方法存根

    }
 

我想问几个问题。上述code未升级。

现在,如果我的应用程序的新用户,我一定要修改旧的数据库和再拍CKRecording表,并与当前数据库放在资产或以上code替换它将会为新用户的工作呢?

解决方案

 超(背景下,DB_NAME,NULL,1);
 

在此声明中的最后一个参数是数据库版本。将其设置为2 作为新更新的APK。所以现有的用户可以有新加入的表或列。

在更新APK onUpgrade将调用。在那里你可以从资产复制数据库。但比已有的用户将有数据丢失。所以最好的办法是添加表动态数据库。

I have an app already in google store.I am using a builtin database having 3 tables and copying it on first launch of app. Now i want to do upgrade to the app and add another table. Below is my code.

public DataBaseHelper(Context context) 
{
    super(context, DB_NAME, null, 1);
    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 database from assests
            copyDataBase();
            Log.e(TAG, "createDatabase database created");
        } 
        catch (IOException mIOException) 
        {
            throw new Error("ErrorCopyingDataBase");
        }
    }
}
    //Check that the database exists here: /data/data/your package/databases/Da Name
    private boolean checkDataBase()
    {
        File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();
    }

    //Copy the database from assets
    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();
    }

    //Open the database, so we can query it
    public boolean openDataBase() throws SQLException
    {
        String mPath = DB_PATH + DB_NAME;
        //Log.v("mPath", mPath);
        mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
        //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        return mDataBase != null;
    }

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        String sql = "CREATE TABLE IF NOT EXISTS CKRecordings (" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                "name TEXT , filepath TEXT , creationDate TEXT)";
        db.execSQL(sql);
        db.execSQL("DROP TABLE IF EXISTS data");
        onCreate(db);
    }

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

    }

I want to ask few questions. The above code is not upgrading.

Now if i am new user of the app, do i have to edit the old database and make another CKRecording table and replacing it with current database placed in asset or above code will work for new users too?

解决方案

super(context, DB_NAME, null, 1);

in this statement last argument is database version.. set it to 2 for new updated APK. so existing user can have newly added table Or Column.

on updated apk onUpgrade will call. and there you can copy database from asset. but than exisiting user will have loss of data.. so best option is to add table dynamically in database.

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

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