android
  • upgrade
  • transfer
  • alter
  • sqlite
  • 2012-07-12 52 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); 
         } 
    
    相關問題