Android Sqlite数据库onupgrade()未更新数据库

Android Sqlite数据库onupgrade()未更新数据库,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,我是安卓应用开发者的新手。我需要帮助我的onupgrade()方法在这里不起作用。任何人都可以帮我 提供完整的java代码 我的问题是,如果我将数据库版本1更改为2并重新生成并运行,那么我的数据将显示为旧数据。我需要在应用程序中更新数据。 有人能帮我吗 谢谢 public class DataHandler extends SQLiteOpenHelper { // Logcat tag private static final int DATABASE_VERSION = 1

我是安卓应用开发者的新手。我需要帮助我的onupgrade()方法在这里不起作用。任何人都可以帮我

提供完整的java代码

我的问题是,如果我将数据库版本1更改为2并重新生成并运行,那么我的数据将显示为旧数据。我需要在应用程序中更新数据。 有人能帮我吗

谢谢

public class DataHandler extends SQLiteOpenHelper {

    // Logcat tag
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "status";
    private static final String DB_PATH = "/data/data/com.gracy.learnstatus/databases/";
    // Table Names
    private static final String STATUS_TABLE_NAME = "mystatusall";
    // Common column names
    private static final String KEY_STATUS_ID = "_id";
    private static final String KEY_STATUS_TAG = "tag";
    private static final String KEY_STATUS_STATUS = "name";
    private static final String KEY_STATUS_FAV = "fav";
    // Table Create Statements
    private static final String STATUS_TABLE_CREATE = "CREATE TABLE "
            + STATUS_TABLE_NAME + "("
            + KEY_STATUS_ID + " INTEGER PRIMARY KEY, "
            + KEY_STATUS_TAG + " TEXT,"
            + KEY_STATUS_STATUS + " TEXT, "
            + KEY_STATUS_FAV + " INTEGER "
            + ")";
    private static final String[] STATUS_COLUMNS = new String[]{
            KEY_STATUS_ID, KEY_STATUS_TAG
            , KEY_STATUS_STATUS, KEY_STATUS_FAV
    };
    private static final String TAG = DataHandler.class.getSimpleName();
    private Context context;

    // constructors
    public DataHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
        try {
            createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(STATUS_TABLE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //  Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + STATUS_TABLE_NAME);
        try {
            createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // create new tables
        onCreate(db);
    }

    // Create Check And Copy Database
    // Creates a empty database on the system and rewrites it with your own database.
    public void createDataBase() throws IOException {

        boolean dbExist = checkDataBase();

        if (dbExist) {
            Log.i(TAG, "database created");
            //do nothing - database already exist
        } 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.getReadableDatabase();
            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 + DATABASE_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        } catch (SQLiteException e) {
            //database does't exist yet.
        }
        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null;
//        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.
     */
    public void copyDataBase() throws IOException {

        //Open your local db as the input stream
        InputStream myInput = context.getAssets().open(DATABASE_NAME);
        // Path to the just created empty db
        String outFileName = DB_PATH + DATABASE_NAME;
        File file = new File(outFileName);
        if (file.delete()) {
            Log.e(TAG, "DB Deleted");
        }

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


}

问题是,若您可以更改数据库相关的函数或代码,那个么以前的应用程序版本就无法更新新的数据库更新任务,所以


您需要卸载以前的应用程序并重新运行项目

问题是,如果您可以更改数据库相关的功能或代码,那么您以前的应用程序版本将无法更新新的数据库更新任务,所以


您需要卸载以前的应用程序并重新运行项目

通过使用断点或日志检查您的onUpgrade方法是否正在调用。您正在从
资产
复制数据库和创建自己的数据库之间混为一谈……正如USKMobility所说,请确定在更改版本号时是否实际调用了onUpgrade。如果遇到异常,onUpgarde将回滚,这可能是问题所在。就我个人而言,我使用另一种方法,使用自定义方法(onExpand),每次应用程序启动时都会运行该方法(基本上是将实际结构与所需结构进行比较,并相应地添加表和列(甚至所有表和列都表示在删除应用程序数据之后))。检查您的mainactivity和DataHandler是否被调用。通过使用断点或日志检查您的onUpgrade方法是否正在调用。您正在从
资产
复制数据库和创建自己的数据库之间混在一起……正如USKMobility所说,在更改版本号时,请确定是否实际调用了onUpgrade。如果遇到异常,onUpgarde将回滚,这可能是问题所在。就我个人而言,我使用另一种方法,使用自定义方法(onExpand),每次应用程序启动时都会运行该方法(基本上是将实际结构与所需结构进行比较,并相应地添加表和列(甚至所有表和列都表示在删除应用程序数据之后))。请检查mainactivity和DataHandler是否被调用。我需要在新版本上升级数据库。我需要在新版本上升级数据库。