将数据传输到新的数据库版本android

时间:2012-07-12 00:01:03

标签: android upgrade transfer alter sqlite

我正在尝试编写一个函数,它将获取数据库中的当前信息,并使用onUpgrade方法将其放入新版本中。这就是我所拥有的。

public void updateTable(SQLiteDatabase db) {
    Cursor c = db.rawQuery(
            "select DISTINCT tbl_name from sqlite_master where tbl_name = '"
                    + tableName + "'", null);
    if (c != null && c.getCount() > 0) {
        c = db.rawQuery("select * from " + tableName, null);
        ArrayList<Transaction> tempList = new ArrayList<Transaction>();
        while (c.moveToNext()) {
            Transaction t = createTransaction(c);
            if (t != null)
                tempList.add(t);
        }
        dropTable(db);
        createTable(db);
        for (Transaction t : tempList) {
            addOccurrence(t);
        }
    } else {
        throw new RuntimeException("Couldn't find new table for updating");
    }
}

此方法接收提供给onUpgrade方法的数据库。

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  switch (newVersion) {
    case NEW_VERSION_NUMBER:
      transTable.updateTable(db);
    break;
  }
}

问题是,我从onUpgrade方法获得的数据库中没有任何内容!所以我最终删除了我的数据库....有没有办法做到这一点,或者我必须依赖有限的ALTER语句使用。感谢。

1 个答案:

答案 0 :(得分:0)

public void onUpgrade(SQLiteDatabase new_db, int oldVersion,
                int newVersion) {
            // TODO Auto-generated method stub
            System.out.println("onUpgrade()");
            new_db.execSQL("DROP TABLE IF EXISTS " + TableName + ";");
            onCreate(new_db);
        }
相关问题