Android Sqlit Database upgrade, add fields

Source: Internet
Author: User

Read many of the online information about database upgrades, field attributes, and learn about:

In fact, the database upgrade has two reliable methods, (1)

db.execsql ("ALTER TABLE test ADD COLUMN Age");
  (2)
Db.begintransaction ();Db.execsql ("ALTER TABLE test RENAME to _test");Db.execsql ("CREATE TABLE Test (ID integer primary key autoincrement, PassWord VARCHAR () not NULL,"+ "UserName varchar (+) NOT NULL, age VARCHAR (+) is not null)");Db.execsql ("INSERT into Test SELECT ID," + str + str+ ", name, age from _test");Db.settransactionsuccessful ();Db.endtransaction ();
Because the database that comes with Android is used by a Sqliteopenhelper class, the OnCreate () is executed at the first call, where the database table is created
  public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {} is called when the current version of the database is less than the new database version
You can upgrade the database while you are here.
  Method one is to add a new field to the table;
  The second method is to modify the name of the current database table, as a temporary table, create a database table to insert the data in the temporary table, so that the data can be upgraded.
Download Demo:
Click to open link

Android Sqlit Database upgrade, add fields

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.