Android的什么是easist方式升级数据库对现有数据库数据库、方式、Android、easist

由网友(念、夕夏温存)分享简介:我HAVA应用程序,它具有数据库,之后出版。我有添加一些表到我的数据库。我如何能轻松地更新与当客户更新之前已经安装了手机市场上的这种应用旧的数据库新数据库。下面是我的code。我应该写在 onUpgrade 公共类DataBaseHelper扩展SQLiteOpenHelper    {    //我们的目标设备上...

我HAVA应用程序,它具有数据库,之后出版。我有添加一些表到我的数据库。

如何能轻松地更新与当客户更新之前已经安装了手机市场上的这种应用旧的数据库新数据库。

下面是我的code。我应该写在 onUpgrade

 公共类DataBaseHelper扩展SQLiteOpenHelper    {    //我们的目标设备上的数据库路径(位置)    私人静态字符串DB_PATH =;    私人静态字符串DB_NAME =sorubankasi.sqlite; //数据库名称    私人SQLiteDatabase mDataBase;    私人最终上下文mContext;    私有静态最终诠释DATABASE_VERSION = 2; //新versiyon    私有静态最后的字符串标记=STK    公共DataBaseHelper(上下文的背景下)    {        超(背景下,DB_NAME,空,DATABASE_VERSION); // 1?它的旧数据库版本        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);            //Log.v("dbFile,DBFILE ++ dbFile.exists());            返回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;            //Log.v("mPath的mpath);            mDataBase = SQLiteDatabase.openDatabase(的mpath,空,SQLiteDatabase.CREATE_IF_NECESSARY);            // mDataBase = SQLiteDatabase.openDatabase(的mpath,空,SQLiteDatabase.NO_LOCALIZED_COLLATORS);            Log.d(标签的openDatabase+ mDataBase.getVersion());            返回mDataBase!= NULL;        }        @覆盖        公共同步无效的close()        {            如果(mDataBase!= NULL)                mDataBase.close();            super.close();        }        @覆盖        公共无效的onCreate(SQLiteDatabase为arg0){        }        @覆盖        公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){        }} 

解决方案

好吧,我已经解决了我的路!只是加入一些codeS中createDatabase方法。

 公共无效的CreateDatabase()抛出IOException    布尔mDataBaseExist = checkDataBase();    如果(!mDataBaseExist){        this.getReadableDatabase();        this.close();        尝试{            copyDataBase();        }赶上(IOException异常mIOException){            抛出新的错误(ErrorCopyingDataBase);        }        }    其他{        SQLiteDatabase分贝= getReadableDatabase();        如果(db.getVersion()&下; DATABASE_VERSION){        this.getReadableDatabase();        this.close();        尝试{            copyDataBase();        }赶上(IOException异常mIOException){            抛出新的错误(ErrorCopyingDataBase);        }        }    }} 
android 怎么连接数据库

毕竟,我认为最好的办法是升级更改数据库的名称。

I hava app, and it has database, after published. I have add some tables to the my database.

How can I easily update new database with old database which was already installed phone before when a customer update this application on market.

Here is my Code. What should I write in onUpgrade?

public class DataBaseHelper extends SQLiteOpenHelper 
    { 

    //destination path (location) of our database on device 
    private static String DB_PATH = "";  
    private static String DB_NAME ="sorubankasi.sqlite";// Database name 
    private SQLiteDatabase mDataBase;  
    private final Context mContext; 
    private static final int DATABASE_VERSION = 2;// new versiyon
    private static final String tag = "stk"; 

    public DataBaseHelper(Context context)  
    { 
        super(context, DB_NAME, null, DATABASE_VERSION);// 1? its old Database Version 
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 
        this.mContext = context; 
    }    

    public void createDataBase() throws IOException 
    { 

        boolean mDataBaseExist = checkDataBase(); 
        if(!mDataBaseExist) 
        { 
            this.getReadableDatabase(); 
            this.close(); 
            try  
            { 
                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); 
            //Log.v("dbFile", dbFile + "   "+ dbFile.exists()); 
            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); 

            Log.d(tag, "opendatabase " + mDataBase.getVersion());
            return mDataBase != null; 
        } 

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

        @Override
        public void onCreate(SQLiteDatabase arg0) {

        }

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

        }}

解决方案

Well I have solved with my way! just adding some codes in to createDatabase method.

public void createDataBase() throws IOException {

    boolean mDataBaseExist = checkDataBase();
    if (!mDataBaseExist) {

        this.getReadableDatabase();
        this.close();
        try {
            copyDataBase();
        } catch (IOException mIOException) {
            throw new Error("ErrorCopyingDataBase");
        }
        }
    else{
        SQLiteDatabase db = getReadableDatabase();
        if(db.getVersion()<DATABASE_VERSION){
        this.getReadableDatabase();
        this.close();
        try {
            copyDataBase();
        } catch (IOException mIOException) {
            throw new Error("ErrorCopyingDataBase");
        }
        }
    }

}

After all, I think best way is for upgrade is changing the database name..

阅读全文

相关推荐

最新文章