使用带有Android应用现有的数据库时,数据库升级 [英] Upgrading DB when using existing Database with Android App

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

问题描述

我用我现有的数据库与我的Andr​​oid应用程序的接触。它工作正常的第一次。如果我升级了资产的文件夹的数据库,并重新安装应用程序数据库不会升级它显示在模拟器中运行的应用程序中旧的。

如何,如果我在使用现有的数据库的情况下改变应用程序的每一个版本的DB版的 onUpgrade()方法的工作?

下面是我的 DataBaseHelper.java

 进口的java.io.File;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.SQLException;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.util.Log;公共类DataBaseHelper扩展SQLiteOpenHelper
{
    私有静态字符串标记=DataBaseHelper; //标签只是为了LogCat中窗口
    //我们的目标设备上的数据库路径(位置)
    私人静态字符串DB_PATH =;
    私人静态字符串DB_NAME =SBLdata.db; //数据库名称
    私人静态字符串DATABASE_TABLE =SBL_Contact; //数据库名称
    私有静态最终诠释DATABASE_VERSION = 1;
    私人SQLiteDatabase mDataBase;
    私人最终上下文mContext;
    私人SQLiteDatabase分贝;
    私人诠释oldVersion;
    私人诠释静态网页;    公共DataBaseHelper(上下文的背景下,字符串DBNAME)
    {
        超(背景下,DB_NAME,空,DATABASE_VERSION);
        如果(android.os.Build.VERSION.SDK_INT> = 17){
            DB_PATH = context.getApplicationInfo()DATADIR +/数据库/。
        }
        其他
        {
            DB_PATH =/数据/数据​​/+ context.getPackageName()+/数据库/;
        }
        this.mContext =背景;
    }    公共无效的CreateDatabase()抛出IOException异常
    {
        //如果数据库不存在从资产复制        布尔mDataBaseExist = checkDataBase();
        如果(!mDataBaseExist)
        {
            this.getReadableDatabase();
            this.close();
            尝试
            {
                //复制从资产产生数据库
                copyDataBase();
            }
            赶上(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);
        字节[] = 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;
        mDataBase = SQLiteDatabase.openDatabase(的mpath,空,SQLiteDatabase.CREATE_IF_NECESSARY);
        返回mDataBase!= NULL;
    }    @覆盖
    公共同步无效的close()
    {
        如果(mDataBase!= NULL)
            mDataBase.close();
        super.close();
    }    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){}   @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        如果(oldVersion< D​​ataBaseHelper.DATABASE_VERSION){
            //删除旧表,并创建和复制新
        }其他{
            //没做什么
        }
    }}


解决方案

在里面onUpgrade(做什么)是高达你。你可以创建一个新的数据库或升级现有的任何你想要的方式。这意味着你必须手动删除旧表并把这些数据插入到一个新表。或使用ALTER_TABLE。

I am using my existing Database with my android contact app. It works fine for the first time. If I upgrade the Asset folder's Database and reinstall the app the Database doesn't upgrade it show the old one during the app run on emulator.

How the onUpgrade() method works if I change the DB version on every release of app in case of using existing Database?

Here is my DataBaseHelper.java

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper
{
    private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
    //destination path (location) of our database on device
    private static String DB_PATH = "";
    private static String DB_NAME ="SBLdata.db";// Database name
    private static String DATABASE_TABLE ="SBL_Contact";// Database name
    private static final int DATABASE_VERSION = 1;
    private SQLiteDatabase mDataBase;
    private final Context mContext;
    private SQLiteDatabase db;
    private int oldVersion;
    private int newVersion;

    public DataBaseHelper(Context context, String dbName)
    {
        super(context, DB_NAME, null, DATABASE_VERSION );
        if(android.os.Build.VERSION.SDK_INT >= 17){
            DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
        }
        else
        {
            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();
            }
            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;
        mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
        return mDataBase != null;
    }

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

    @Override
    public void onCreate(SQLiteDatabase db) {}

   @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion < DataBaseHelper.DATABASE_VERSION) {
            //drop old table and create and copy the new one
        }else{
            //do Nothing
        }
    }

}

解决方案

what you do inside onUpgrade() is upto you. you can create a new database or upgrade existing in any way you want. that means you have to manually drop the old table and insert these data into a new table. or use ALTER_TABLE.

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

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