Database Creation under Android

Source: Internet
Author: User

The databases used under Android are:SQLite, embedded lightweight

To create a SQLite database is generally to inherit the Sqliteopenhelper class.

The official documentation is this:

Since it is an abstract class that must be inherited with a class, there are two abstract methods OnCreate and Onupgrade must be implemented.

OnCreate method : is called when the database is first created (but is not called when created), especially for table structure initialization, requires SQL statement

Onupgrade Method : Database version number changes will be executed, especially for the table structure changes, after Android4.0 version can only increase or decrease, or there will be an exception

The following creates a Mysqliteopenhelper class to inherit from the Sqliteopenhelper class.

1  Public classMysqliteopenhelperextendsSqliteopenhelper {2 3      PublicMysqliteopenhelper (Context context) {4         //Context: Contexts Name: Database file names Factory: Used to create cursor objects, default NULL5         //version: The release number of the database, starting with 1, if a change occurs, the Onupgrade method calls6         Super(Context, "starry.db",NULL, 2);7     }8 @Override9      Public voidonCreate (Sqlitedatabase db) {Ten          OneDb.execsql ("CREATE table info (_id integer primary key autoincrement, name varchar (20))"); A     } - @Override -      Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { theDb.execsql ("ALTER TABLE info add phone varchar (11)"); -     } -  -}
View Code

When you instantiate a mysqliteopenhelper in Mainactivity.java, you can initialize the database by running the Getreadabledatabase method.

1     New Mysqliteopenhelper (mcontext); 2     Mysqliteopenhelper.getreadabledatabase ();
View Code

Database Creation under Android

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.