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

时间:2018-02-16 13:08:50

标签: android sqlite android-sqlite

Android SQLite帮助文件具有以下方法。我通过添加一列来更新我的数据库。数据库已升级,但更改未生效。我更改了版本代码,但仍无效果。 未调用onUpgrade方法。这是为什么?如果必须使用任何可写权限,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");


    }

}

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-2)

当您更改数据库结构并且想要执行onUpdate时,您需要更改数据库版本。为此,您需要更改构造函数中执行的super()方法的第三个参数中的数字。它应该是这样的:

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)