onUpgrade不会被调用,在第一次的android SQLiteOpenHelper [英] onUpgrade is not being called at first time in android SQLiteOpenHelper

查看:461
本文介绍了onUpgrade不会被调用,在第一次的android SQLiteOpenHelper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个很奇怪的问题。我开发一个Android应用程序,有一个SQLite数据库作为其数据库。我使用它扩展SQLiteOpenHelper DBAdapter类。我搜索在互联网上,但无法找到一个解决方案。

I am facing a very strange problem. I am developing an android application that has an sqlite DB as its database. I am using DBAdapter class which extends SQLiteOpenHelper. I have searched on internet but can't find a solution.

下面是我SQLiteOpenHelper类

Here is my SQLiteOpenHelper class

public class DBAdapter  extends SQLiteOpenHelper{

         private static DBAdapter mInstance = null;
        /**The Android's default system path of your application database. */
        private static String DB_PATH = "/data/data/com.mydbapp.android/databases/";

        private final static String DB_NAME = "mydb.sqlite";
        private static final int DATABASE_VERSION = 1;
        private static Context myContext;    


        public static DBAdapter getInstance(Context ctx) {

            if (mInstance == null) {
                mInstance = new DBAdapter(ctx);
            }
            return mInstance;
          }
            private DBAdapter(Context context) {     
                super(context,  DB_NAME, null, DATABASE_VERSION);
                DBAdapter.myContext = context;
                     DB_PATH = "/data/data/" +
                             context.getPackageName()+
                "/databases/"; 
            }   

           public void deleteDB()
           {
               myContext.deleteDatabase(DB_NAME);
           }

            public void createDataBase() throws IOException{     
                boolean dbExist = checkDataBase();  
                if(dbExist )
                {
                   this.getWritableDatabase();
                }
                if(!dbExist){
                    this.getReadableDatabase();
                    try {     
                        copyDataBase();
                    } catch (IOException e) {     
                        e.printStackTrace();
                        throw new Error("Error copying database");     
                    }
                }
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
            {
                if (newVersion > oldVersion)
                {
                            System.out.println("DB Upgrade logic")
                        }
            }

现在,当我修改私有静态最终诠释DATABASE_VERSION = 2; 然后 onUpgrade()方法并没有得到调用,但是当我改变私有静态最终诠释DATABASE_VERSION = 3; 然后onUpgrade()的作品。 所以我的问题是,为何它没有调用 onUpgrade()方法,当我改变数据库的版本从1到2。 请大家帮我解决这个问题。

Now when I change private static final int DATABASE_VERSION = 2; then onUpgrade() method doesn't get invoked, but when I change private static final int DATABASE_VERSION = 3; then onUpgrade() works. So my question is that why doesn't it invoke the onUpgrade() method when I change DB version from 1 to 2. Please help me resolve this issue.

我注意到多了一个奇怪的现象。当我最初运行的应用程序与数据库的版本1,然后再安装我用理智DB版(1)应用程序,现在如果我改变DB版本1〜2,然后onUpgrade()被调用。我的意思是说,我要安装应用程序2次相同的数据库版本,那么如果我改变数据库的版本,onUpgrade()被调用。

I have noticed one more strange behavior. When I initially run application with DB version 1 and then again I install application with sane DB version (1) and now if I change DB version 1 to 2, then onUpgrade() is called. What I mean to say is that I have to install application 2 times with same DB version then if I change DB version, onUpgrade() is called.

推荐答案

试试这个方法

在使用 DBHelper 类, onUpgrade() mehtod会自动在您更改数据库版本的调用。你不需要与新老版本比较。你可以删除的版本比较。它会帮助你。检查以下code更多的clearification。

When you use DBHelper class, onUpgrade() mehtod will be automatically called when you change db version. You don't need to compare with old and new version. you can remove comparison of version. it will help you. check below code for more clearification.

public class DBHelper extends SQLiteOpenHelper {

    // Static Final Variable database meta information

    static final String DATABASE = "empapp.db";
    static final int VERSION = 1;
    static final String TABLE = "emp";
    static final String TABLE_DEPT = "dept";

    static final String C_ID = "_id";
    static final String C_ENAME = "ename";
    static final String C_DESIGNATION = "designation";
    static final String C_SALARY = "salary";

   // Override constructor
    public DBHelper(Context context) {
        super(context, DATABASE, null, VERSION);

   }

    // Override onCreate method
    @Override
    public void onCreate(SQLiteDatabase db) {

        // Create Employee table with following fields
        // _ID, ENAME, DESIGNATION and SALARY
        db.execSQL("CREATE TABLE " + TABLE + " ( " + C_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + C_ENAME + " text, "
            + C_DESIGNATION + " text, " + C_SALARY + " text )");
    }

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

        // Drop old version table
        db.execSQL("Drop table " + TABLE);

        // Create New Version table
        onCreate(db);
    }

}

这篇关于onUpgrade不会被调用,在第一次的android SQLiteOpenHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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