简体   繁体   English

Android SQLite数据库的onUpgrade方法没有调用吗?

[英]Android SQLite database onUpgrade method is not called?

Android SQLite helper file has the following methods. Android SQLite帮助程序文件具有以下方法。 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. 不调用onUpgrade方法。 Why is that? 这是为什么? Is there anything wrong with the checkdatabase() method where any writable permission must be used. checkdatabase()方法有什么问题,必须使用任何可写权限。

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. 我将openDatabase()方法从数据库的返回值更改为对我有用的this.getWritableDatabase()。

 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. 当您更改数据库结构并想要执行onUpdate时,您需要更改数据库版本。 To do so you need to change the number in the third parameter of super() method that is executed in your constructor. 为此,您需要更改在构造函数中执行的super()方法的第三个参数中的数字。 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) ref: https : //developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#SQLiteOpenHelper (android.content.Context,java.lang.String,android.database.sqlite.SQLiteDatabase.CursorFactory,int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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