Database data migration issues related to Android version upgrade

Source: Internet
Author: User
<span id="Label3"></p><p><p>recently, the old version has been upgraded to the new version, the new version has added several tables, and some of the tables have been added FIELDS. So how do you move old data into a new version?</p></p><p><p><br></p></p><p><p>I wrote a pseudo-code about the user login information in our project, the users table, history (user-viewed posts forum, favorite posts Forum)</p></p><p><p><br></p></p><p><p>I also involve the invocation of the Interface.</p></p><p><p>It is also stated that this code can not be called in the Onupdategrade (), the specific reasons I do not know. In addition, the structure of the table changes, the transaction is not Good.</p></p><p><p>#是注释部分, the transaction was eventually commented Out.<br></p></p><p><br>User user = null;<br>If (login) {<br><br>try{<br>#请求获取好友资料接口<br>User user = Helper.getuserinfo (xxx);<br><br><br>#设置 Datacache.islogin = true;<br>#设置 Datacache.user Call Updateuserinfo ();<br><br><br><br>}catch{<br>#设置sp里 db_move_133_to_201 = false;<br>Datacache.islogin = false;<br>}<br><br>}else{<br>Datacache.islogin = false;<br>user = null;<br><br>}<br><br>#整个操作在事务中完成<br>Sqlitedatabase db = ....;<br>#db. BeginTransaction ();//start Transaction<br><br>#迁移Users<br> <br><br>#1. Change the Users table to a temporary table temp_users<br><br>ALTER TABLE Users RENAME to users_temp;<br><br>#2. Create a new table<br><br>CREATE TABLE Users (_id INTEGER PRIMARY KEY autoincrement,<br>UserName varchar () not null,<br>LoginName varchar () not null,<br>Weiwang varchar () not null,<br>Iscarowner varchar () not null,<br>Sex varchar (not null),<br>Provinceid varchar () not null,<br>Provincename varchar () not null,<br>Cityid varchar () not null,<br>CityName varchar () not null,<br>Score varchar () not null,<br>UserID varchar (not null),<br>Rigisttime varchar () not null,<br>Isautologin int not null,<br>PWD int not null,<br>Userpic varchar () not null,<br>MemberID varchar () not null,<br>Key varchar ($) not null);<br><br><br>#3. Import data<br><br><br>INSERT into Users SELECT _id,username,loginname, ' 0 ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ' isautologin,pwd Temp<br><br>#4. Delete temporary table<br><br>DROP TABLE Users_temp<br><br><br>#迁移history数据步骤<br><br>#1. Change the history table to a temporary table temp_history<br><br>ALTER TABLE history RENAME to history_temp;<br><br>#2. Create a new table<br>CREATE TABLE History (_id INTEGER PRIMARY KEY autoincrement,<br>UserID varchar (+) NOT NULL default ' 0 ',<br>DataSource varchar () NOT null default ' 0 ',<br>handleaction varchar () NOT NULL default ' 0 ',<br>Historyid varchar () not null,<br>Title varchar (not null),<br>Bbsid varchar () not null,<br>Bbstype varchar () not null,<br>typeID varchar () not null,<br>Viewtime varchar () not null);<br><br>#3. Import data<br> <br>INSERT into history SELECT _id, ' 0 ', ' 0 ', ' 0 ', historyid,title,bbsid,bbstype,typeid,viewtime from history_temp;<br><br><br>#4. If the user is logged in, the UserID field is updated<br><br>UPDATE History Set userid=?;<br><br>For example: UPDATE history set userid=587766;<br><br>#5. Delete temporary table<br><br>DROP TABLE History_temp<br><br><br>#db. settransactionsuccessful ();//call This method to commit the current transaction when it executes to endtransaction (), and if this method is not called, the transaction is rolled back<br><br>#设置sp里 db_move_133_to_201 = true;<br><br>} finally {<br>#db. endtransaction ();//the Flag of the transaction determines whether the transaction is committed or rolled back<br>}<br>Db.close ();</p><p><p>Database data migration issues related to Android version upgrade</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.