SQLite数据从版本0迁移到1

时间:2017-09-20 07:44:31

标签: android android-sqlite sqliteopenhelper

以前我使用db = openOrCreateDatabase("LOCALTILEDB", Context.MODE_PRIVATE, null);创建了一个数据库,它正在创建一个版本为'0'的数据库。

在下一个应用程序版本中,我使用的是SQLiteOpenHelper类,其中OnUpgrade方法显示调用的错误,数据库版本不能为'0'。

所以请任何人帮我将数据从我的0版DB迁移到版本1 DB。

2 个答案:

答案 0 :(得分:0)

在使用SQLiteOpenHelper扩展的类中,在默认构造函数上传递super(context,DATABASE_NAME,null,DATABASE_VERSION)。对于前。

public class MyDBHelper extends SQLiteOpenHelper {

// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "MyDb";


public MyDBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase aSqLiteDatabase) {

}  @Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

} 

}

答案 1 :(得分:0)

我怀疑你所做的是编写通过super传递的0版本。 : -

public static final int DBVERSION = 0;

public SO46316125DBHlpr(Context context) {
    super(context, DBNAME, null, DBVERSION);
}

这将导致包含Version must be >= 1, was 0

的异常

e.g。 : -

09-20 21:16:40.631 1840-1840/mjt.soqanda E/AndroidRuntime: FATAL EXCEPTION: main
                                                           Process: mjt.soqanda, PID: 1840
                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{mjt.soqanda/mjt.soqanda.MainActivity}: java.lang.IllegalArgumentException: Version must be >= 1, was 0
                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                               at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                               at android.os.Looper.loop(Looper.java:135)
                                                               at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                            Caused by: java.lang.IllegalArgumentException: Version must be >= 1, was 0
                                                               at android.database.sqlite.SQLiteOpenHelper.<init>(SQLiteOpenHelper.java:99)
                                                               at android.database.sqlite.SQLiteOpenHelper.<init>(SQLiteOpenHelper.java:77)
                                                               at mjt.soqanda.SO46316125DBHlpr.<init>(SO46316125DBHlpr.java:23)

然而,简单地编码传递给super方法的非零正面版本将导致使用该版本。

e.g。我已经将数据库设置为版本0(使用PRAGMA user_version=0,然后是PRAGMA user_version(检索版本)并且具有: -

09-20 21:29:35.117 19172-19172/? D/DBVERSION: The Database User Version is 1
09-20 21:29:35.118 19172-19172/? D/CHGDBVERSION: Attempting to alter DB Version from 1 to new version 0
09-20 21:29:35.152 19172-19172/? D/CHGDBVERSION: DB Version changed successfully, DB Version is now 0

然后我使用版本100: -

public static final int DBVERSION = 100;

public SO46316125DBHlpr(Context context) {
    super(context, DBNAME, null, DBVERSION);
}

因此,根据以下内容直接跳转到版本100(onUpgrade方法为空): -

09-20 21:35:45.344 27937-27937/mjt.soqanda D/DBVERSION: The Database User Version is 100

当然1也有效。

为了测试/生产以上方法,我使用了以下方法: -

public void showDBVersion() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor csr = db.rawQuery("PRAGMA user_version",null);
    if (csr.moveToFirst()) {
        Log.d("DBVERSION","The Database User Version is " + csr.getString(0));
    }
}


public int alterDBVersion(SQLiteDatabase db, int fromversion, int toversion) {

    int current_version = -1;
    int changed_version = -1;

    Cursor csr = db.rawQuery("PRAGMA user_version;",null);
    if (csr.moveToFirst()) {
        current_version = csr.getInt(0);
    } else {
        Log.d("CHGDBVERSION","Unable to get the current version.");
        return current_version;
    }

    if (current_version == fromversion) {
        Log.d("CHGDBVERSION","Attempting to alter DB Version from " +
                Integer.toString(fromversion) +
                " to new version " +
                Integer.toString(toversion)
        );
        db.rawQuery("PRAGMA user_version=" + Integer.toString(toversion) + ";",null); //NOTE 1
        db.execSQL("PRAGMA user_version=" + Integer.toString(toversion) + ";");
    }

    csr = db.rawQuery("PRAGMA user_version;",null);
    if (csr.moveToFirst()) {
        changed_version = csr.getInt(0);
    } else {
        Log.d("CHGDBVERSION","Unable to get the new/changed version.");
        return -2;
    }
    if (current_version != fromversion || changed_version != toversion) {
        Log.d("CHGDBVERSION",
                "DB Version was not changed as requested. Version is now " +
                        Integer.toString(changed_version));
        return -3;
    } else {
        Log.d("CHGDBVERSION","DB Version changed successfully, DB Version is now " +
        Integer.toString(changed_version));
    }
    return 0;
}

注1不起作用必须按照Android: PRAGMA statements that set data don't work

使用execSQL

使用MainActivity中的以下代码

    SO46316125DBHlpr dbhlpr = new SO46316125DBHlpr(this);
    dbhlpr.showDBVersion();
    dbhlpr.alterDBVersion(dbhlpr.getWritableDatabase(),1,0);

显然改变参数(首先是要更改的版本,因此当前版本必须与之匹配,第二个是要更改的版本,如果第一个匹配)。上面的示例将版本1更改为版本0。