【问题标题】:Transfer data to new database version android将数据传输到新的数据库版本android
【发布时间】:2012-07-12 00:01:03
【问题描述】:

我正在尝试编写一个函数,它将获取数据库中的当前信息并使用 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 语句的有限使用。谢谢。

【问题讨论】:

    标签: android upgrade transfer alter sqlite


    【解决方案1】:
    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);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 2014-05-29
      • 1970-01-01
      • 2011-09-12
      • 2015-03-05
      • 2011-11-26
      • 1970-01-01
      相关资源
      最近更新 更多