android
  • upgrade
  • transfer
  • alter
  • sqlite
  • 2012-07-12 62 views 0 likes 
    0

    我想写一个函数,它将采用数据库中的当前信息并将其放入带有onUpgrade方法的新版本中。这是我的。传输数据到新的数据库版本android

    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语句的有限使用。谢谢。

    回答

    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); 
         } 
    
    相关问题