简体   繁体   中英

Android SQLite database onUpgrade method is not called?

Android SQLite helper file has the following methods. I updated my database by adding a column. The DB is upgraded but the changes are not coming into effect. I have changed the version code but still no effect. The onUpgrade method is not called. Why is that? Is there anything wrong with the checkdatabase() method where any writable permission must be used.

public class ExternalDbOpenHelper extends SQLiteOpenHelper {

public static String DB_PATH;

public static String DB_NAME;
public SQLiteDatabase database;
public final Context context;

public SQLiteDatabase getDb() {
    return database;
}

public ExternalDbOpenHelper(Context context, String databaseName) {
    super(context, databaseName, null, 2);
    this.context = context;
    //String version_db = ;

    DB_NAME = databaseName;
    String path = context.getDatabasePath(DATABASE_NAME).getPath();
    DB_PATH = path;
    openDataBase();

}


public void createDataBase() {
    boolean dbExist = checkDataBase();
    if (!dbExist) {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            Log.e(this.getClass().toString(), "Copying error");
            throw new Error("Error copying database!");
        }
    } else {
        Log.i(this.getClass().toString(), "Database already exists");
    }
}

private boolean checkDataBase() {
    SQLiteDatabase checkDb = null;
    try {
        String path = DB_PATH + DB_NAME;
        checkDb = SQLiteDatabase.openDatabase(path, null,
                SQLiteDatabase.OPEN_READONLY);
    } catch (SQLException e) {
        Log.e(this.getClass().toString(), "Error while checking db");
    }

    if (checkDb != null) {
        checkDb.close();
    }
    return checkDb != null;
}

private void copyDataBase() throws IOException {

    InputStream externalDbStream = context.getAssets().open(DB_NAME);


    String outFileName = DB_PATH + DB_NAME;


    OutputStream localDbStream = new FileOutputStream(outFileName);


    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = externalDbStream.read(buffer)) > 0) {
        localDbStream.write(buffer, 0, bytesRead);
    }

    localDbStream.close();
    externalDbStream.close();

}

public SQLiteDatabase openDataBase() throws SQLException {
    String path = DB_PATH + DB_NAME;
    if (database == null) {
        createDataBase();
        database = SQLiteDatabase.openDatabase(path, null,
                SQLiteDatabase.OPEN_READWRITE);
    }
    return database;
}
@Override
public synchronized void close() {
    if (database != null) {
        database.close();
    }
    super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {

     try {
        db.execSQL("ALTER TABLE UserMaster ADD COLUMN scheme_status 
        TEXT");
        db.execSQL("ALTER TABLE UserMaster ADD COLUMN location_status 
        TEXT");

     } catch (Exception Exp) {


    }
}


private Context getBaseContext() {
    // TODO Auto-generated method stub
    return null;
}


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

    if(newVersion > oldVersion){

        db.execSQL("ALTER TABLE UserMaster ADD COLUMN scheme_status TEXT");


    }

}

}

I changed the return of openDatabase() method from database to this.getWritableDatabase() which worked for me.

 public SQLiteDatabase openDataBase() throws SQLException {
 String path = DB_PATH + DB_NAME;
 if (database == null) {
    createDataBase();
    database = SQLiteDatabase.openDatabase(path, null,
            SQLiteDatabase.OPEN_READWRITE);
 }
 return this.getWritableDatabase();
 }

When you change database structure and you would like to execute onUpdate you need to change database version. To do so you need to change the number in the third parameter of super() method that is executed in your constructor. It should be like this:

public ExternalDbOpenHelper(Context context, String databaseName) {
    super(context, databaseName, null, 3);
    this.context = context;
    //String version_db = ;

    DB_NAME = databaseName;
    String path = context.getDatabasePath(DATABASE_NAME).getPath();
    DB_PATH = path;
    openDataBase();

}

ref: https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#SQLiteOpenHelper(android.content.Context , java.lang.String, android.database.sqlite.SQLiteDatabase.CursorFactory, int)

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