Android Learning notes (18)--talk about upgrading the database

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">This series is the "first line of Android code" learning notes, If there are errors and omissions, please correct me!</span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">Before we had to make sure that the tables in the database were up to date, we simply removed all of the current tables in the Onupgrade () method, and then forced the OnCreate () method to be re-executed again. This method can be used in the development phase of the product, but when the product is actually online, it will definitely not Work. Imagine the following scenario, such as an app you've written that has been successfully launched and has a good download volume. Now because of the reason for adding new features, The database also needs to be upgraded together, and then the user updates this version and discovers that the local data stored in the previous program has been Lost.</span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">So we need to do some reasonable control to ensure that the data is not lost when upgrading the Database. We know that each database version corresponds to a version number, and when the specified database version number is greater than the current database version number, it goes into the Onupgrade () method to perform the update Operation. So we can assign each version number to its own change here, then judge the version number of the current database in the Onupgrade () method, then perform the corresponding Change.</span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">Let's go into practice:</span></p></p><p><p><br><span style="font-family: 幼圆; font-size: 16px;">one, Mission one: Add a book table to the database</span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">This is relatively simple, the Mydatabasehelper code is as Follows:</span></p></p><span style="font-family: Microsoft YaHei; font-size: 14px;"><span style="font-family: Microsoft YaHei; font-size: 14px;"></span></span><pre><span style="font-family: Microsoft YaHei; font-size: 14px;"><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mydatabasehelper<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Sqliteopenhelper {</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>String create_book = "CREATE table book ("<span style="color: #008080;"><span style="color: #008080;">3</span></span>+ "id Integer PRIMARY key autoincrement,"<span style="color: #008080;"><span style="color: #008080;">4</span></span>+ "author text,"<span style="color: #008080;"><span style="color: #008080;">5</span></span>+ "price real,"<span style="color: #008080;"><span style="color: #008080;">6</span></span>+ "pages integer,"<span style="color: #008080;"><span style="color: #008080;">7</span></span>+ "name Text")<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">mydatabasehelper (context context, String name, cursorfactory</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span>Factory<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Version) {</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">(context, name, factory, version);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (sqlitedatabase Db) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">Db.execsql (create_book);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onupgrade (sqlitedatabase db,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>oldversion,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Newversion) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>}</span></pre><span class="cnblogs_code_collapse" style="font-family: Microsoft YaHei; font-size: 14px;"><span class="cnblogs_code_collapse" style="font-family: Microsoft YaHei; font-size: 14px;">View Code</span></span><p><p></p></p><p><p><span style="font-family: 幼圆; font-size: 16px;">second, Mission two: Add another category table to the database</span></p></p><span style="font-family: Microsoft YaHei; font-size: 14px;"><span style="font-family: Microsoft YaHei; font-size: 14px;"></span></span><pre><span style="font-family: Microsoft YaHei; font-size: 14px;"><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mydatabasehelper<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Sqliteopenhelper {</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>String create_book = "CREATE table book ("<span style="color: #008080;"><span style="color: #008080;">3</span></span>+ "id Integer PRIMARY key autoincrement,"<span style="color: #008080;"><span style="color: #008080;">4</span></span>+ "author text,"<span style="color: #008080;"><span style="color: #008080;">5</span></span>+ "price real,"<span style="color: #008080;"><span style="color: #008080;">6</span></span>+ "pages integer,"<span style="color: #008080;"><span style="color: #008080;">7</span></span>+ "name Text")<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>String create_category = "CREATE table CATEGORY ("<span style="color: #008080;"><span style="color: #008080;">9</span></span>+ "id Integer PRIMARY key autoincrement,"<span style="color: #008080;"><span style="color: #008080;">Ten</span></span>+ "category_name text,"<span style="color: #008080;"><span style="color: #008080;"></span> one</span>+ "category_code Integer")<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Mydatabasehelper (context context, String name,cursorfactory factory,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Version) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">(context, name, factory, version);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (sqlitedatabase Db) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;">Db.execsql (create_book);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">Db.execsql (create_category);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onupgrade (sqlitedatabase db,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>oldversion,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Newversion) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(oldversion) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span>1<span style="color: #000000;"><span style="color: #000000;">:</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">Db.execsql (create_category);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">:</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> to</span>}</span></pre><span class="cnblogs_code_collapse" style="font-family: Microsoft YaHei; font-size: 14px;"><span class="cnblogs_code_collapse" style="font-family: Microsoft YaHei; font-size: 14px;">View Code</span></span><p><p></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">As you can see, in the OnCreate () method We added a new build statement, and then added a switch in the Onupgrade () method, if the User's current database version number is 1, it will only create a category Table. This creates the two tables together when the user is the second version of the program that is installed Directly. When the user is using the second version of the program to overwrite the installation of the first version of the program, will go into the upgrade database operation, at this time because the Book table already exists, so only need to create a category Table.</span></p></p><p><p></p></p><p><p><span style="font-family: 幼圆; font-size: 16px;">third, Misson three: Add a category_id field to the Book table</span></p></p><span style="font-family: Microsoft YaHei; font-size: 14px;"><span style="font-family: Microsoft YaHei; font-size: 14px;"></span></span><pre><span style="font-family: Microsoft YaHei; font-size: 14px;"><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mydatabasehelper<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Sqliteopenhelper {</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>String create_book = "CREATE table book ("<span style="color: #008080;"><span style="color: #008080;">3</span></span>+ "id Integer PRIMARY key autoincrement,"<span style="color: #008080;"><span style="color: #008080;">4</span></span>+ "author text,"<span style="color: #008080;"><span style="color: #008080;">5</span></span>+ "price real,"<span style="color: #008080;"><span style="color: #008080;">6</span></span>+ "pages integer,"<span style="color: #008080;"><span style="color: #008080;">7</span></span>+ "name text,"<span style="color: #008080;"><span style="color: #008080;">8</span></span>+ "category_id Integer")<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>String create_category = "CREATE table CATEGORY ("<span style="color: #008080;"><span style="color: #008080;">Ten</span></span>+ "id Integer PRIMARY key autoincrement,"<span style="color: #008080;"><span style="color: #008080;"></span> one</span>+ "category_name text,"<span style="color: #008080;"><span style="color: #008080;"></span> a</span>+ "category_code Integer")<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Mydatabasehelper (context context, String name,cursorfactory factory,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Version) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">(context, name, factory, version);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (sqlitedatabase Db) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">Db.execsql (create_book);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;">Db.execsql (create_category);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onupgrade (sqlitedatabase db,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>oldversion,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Newversion) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(oldversion) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span>1<span style="color: #000000;"><span style="color: #000000;">:</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">Db.execsql (create_category);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span>2<span style="color: #000000;"><span style="color: #000000;">:</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span>Db.execsql ("alter table book add column category_id integer"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">:</span></span><span style="color: #008080;"><span style="color: #008080;"></span> to</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</span></pre><span class="cnblogs_code_collapse" style="font-family: Microsoft YaHei; font-size: 14px;"><span class="cnblogs_code_collapse" style="font-family: Microsoft YaHei; font-size: 14px;">View Code</span></span><p><p></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">first, We added a category_id column to the book Table's build statement, so that when the user installs the third version of the program directly, the new column is automatically added successfully. however, If the user has previously installed a version of the program, and now needs to overwrite the installation, it will go to the upgrade database Operation. In the Onupgrade () method, We added a new case, and if the current database version number is 2, an ALTER command will be executed to add a category_id column to the book Table.</span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">Here's one thing to note: the end of every case in switch is not using Break. The reason for this is to ensure that every database modification can be performed at the time of the Cross-version Upgrade. For example, the user is currently upgrading from the second version of the program to the third version of the program, then the logic in case 2 will be executed. If the user is directly from the first version of the program to upgrade to the third version of the program, then case 1 and the logic in case 2 will be executed. Using this approach to maintain a database upgrade, regardless of how the version is updated, ensures that the Database's table structure is up-to-date and that the data in the table is not lost at All.</span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">  </span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">End.</span></p></p><p><p>Android Learning notes (18)--talk about upgrading the database</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.