0

我的应用程序的 SD 卡上有我的 SQlite 表。现在,我必须升级我的数据库。但是,在 onUpgrade 之后,SQLiteOpenHelper 的 onCreate 永远不会被调用。

这是我的代码:

public class DatabaseHandler extends SQLiteOpenHelper {

private static final String DATABASE_NAME = Environment
        .getExternalStorageDirectory().getAbsolutePath()
        + "/"
        + Environment.DIRECTORY_DOWNLOADS + "/MyApp.sqlite";

public void onCreate(SQLiteDatabase db) {
    Log.e(TAG, "DB onCreate");
    // String CREATE_SCHEMA =
    db.execSQL("CREATE TABLE Master ("
            + "  ID integer PRIMARY KEY NOT NULL ,"
            + "  UserName varchar(50) NOT NULL ,"
            + "  DriverID int NOT NULL ,"
            + "  DriverName varchar(100) NULL ,"
            + "  MobilePrint int NOT NULL DEFAULT 0 ,"
            + "  LastSyncUTC text NULL ,"
            + "  SchemaVersion TEXT NOT NULL ,"
            + "  PasswordHash TEXT NULL ," + "  SyncMinutes int NOT NULL ,"
            + "  LatestAppVersion TEXT NOT NULL" + ");");

    @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO: handle updates when they are necessary
    Log.e(TAG, "DB onUpgrade");
    db.execSQL("DROP TABLE IF EXISTS " + "Master");
}

我不明白为什么,它没有升级到新版本。

4

1 回答 1

1

在 onUpgrade 中,您必须完成重新创建数据库的所有工作。onUpgrade 后不会调用 onCreate()。

于 2013-10-07T21:22:29.570 回答