Android 版本升級涉及到的資料庫資料移轉問題

來源:互聯網
上載者:User

標籤:

最近做老版本向新版本升級,新版本增加了幾張表,有的表經過了增加欄位。那麼如何把老的資料移轉到新的版本裡呢?


我寫了一段虛擬碼,是關於我們項目裡的 使用者登入資訊 Users表,    曆史表history(使用者瀏覽過的文章論壇,收藏過的文章論壇)


我裡面還涉及到介面的調用。

另外說明一點,這段代碼是無法在 onUpdateGrade()裡面調用的,具體什麼原因我也不清楚。另外表的結構發生改變,事務也不好使了。

#是注釋部分,事務最終被注釋掉了


User  user = null;
if(登入){
     
   try{
     #請求擷取好友資料介面
     User  user = helper.getUserInfo(xxx);
     
   
      #設定 DataCache.isLogin = true;
      #設定 DataCache.user   調用updateUserInfo();



    }catch{
       #設定sp裡 DB_MOVE_133_TO_201 = false ;
       DataCache.isLogin = false;
    }

  }else{
     DataCache.isLogin = false;
     user = null;

  }

#整個操作在事務中完成
SQLiteDatabase db = ....;
#db.beginTransaction();//開始事務

#遷移Users
 

   #1. 將Users表改為暫存資料表temp_Users
 
        ALTER TABLE Users RENAME TO Users_temp;

   #2. 建立新表

     create table Users (  _id  INTEGER PRIMARY KEY AUTOINCREMENT,
                 userName varchar(50) not null,
                 loginName varchar(50) not null,
                 weiWang varchar(50) not null,
                 iscarowner varchar(50) not null,
                 sex varchar(50) not null,
                 provinceid varchar(50) not null,
                 provincename varchar(50) not null,
                 cityid varchar(50) not null,
                 cityname varchar(50) not null,
                 score varchar(50) not null,
                 userid varchar(50) not null,
                 rigisttime varchar(50) not null,
                                 isAutoLogin int not null,
                 pwd int not null,
                 userPic varchar(50) not null,
                 memberId varchar(50) not null,
                 Key varchar(500) not null);

   
    #3. 匯入資料  
    
     
     INSERT INTO Users SELECT _id,userName,loginName,‘0‘,‘‘,‘‘,‘‘,‘‘,‘‘,‘‘,‘‘,‘‘,‘‘,isAutoLogin,pwd,‘‘,‘‘,Key FROM Users_temp;

    #4. 刪除暫存資料表 
    
    DROP TABLE Users_temp


#遷移history資料步驟

#1. 將history表改為暫存資料表temp_history
   
    ALTER TABLE history RENAME TO history_temp;

#2. 建立新表
  create table history (_id  INTEGER PRIMARY KEY AUTOINCREMENT,
                                         userid varchar(50) not null default ‘0‘,  
                     datasource varchar(12) not null default ‘0‘,  
                     handleaction varchar(12) not null default ‘0‘,  
                     historyId varchar(12) not null,
                     title varchar(50) not null,
                     bbsId varchar(12) not null,
                     bbsType varchar(12) not null,
                     typeid varchar(12) not null,
                     viewtime varchar(50) not null);

#3. 匯入資料  
    
     INSERT INTO history SELECT _id,‘0‘,‘0‘,‘0‘,historyId,title,bbsId,bbsType,typeid,viewtime FROM history_temp;


#4. 若是登入使用者,則更新userid欄位

    UPDATE history set userid=?;

    例如:  UPDATE history set userid=587766;

#5. 刪除暫存資料表 
    
    DROP TABLE history_temp
    
 
   #db.setTransactionSuccessful();//調用此方法會在執行到endTransaction() 時提交當前事務,如果不調用此方法會復原事務
   
   #設定sp裡 DB_MOVE_133_TO_201 = true ;
 
 } finally {
       #db.endTransaction();//由事務的標誌決定是提交事務,還是復原事務
  }
    db.close();    

Android 版本升級涉及到的資料庫資料移轉問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.