简体   繁体   中英

Sqlite Database onupgrade() not updating database

I'm New in android app developer . I need to help my onupgrade() method not working here. any one can help me please.

Full java code provided.

My problem is if i change database version 1 to 2 and rebuild and run then my data displayed old. And i need updated data in app. So anyone can help me?

Thanks.

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


}

Problem is if you can any change in database related function or code then your previous application version can not update new database updated task so..

You need to uninstall previous application and rerun your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM