Sqlite数据库onupgrade()不更新数据库

时间:2016-10-15 06:49:14

标签: android sqlite android-sqlite

我是Android应用开发者的新手。我需要帮助我的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();
    }


}

1 个答案:

答案 0 :(得分:0)

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

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