Detailed description of version control functions in Android sqlitedatabase

Source: Internet
Author: User

 

When Android comes to the database, it comes to think of SQLite and SQLite.SqlitedatabaseAndSqliteopenhelper

In the Android system, we provide a class named sqliteopenhelper, which is used to manage database versions. This class is an abstract class and has two important methods.

Oncreate (sqlitedatabase dB) and onupgrade (sqlitedatabase dB, int oldversion, int newversion)

 

PublicSqliteopenhelper(ContextContext,StringName,Sqlitedatabase. cursorfactoryFactory,
Int Version)

super ( context , db_name , null ,
db_version );
about the database version, set it to 1.
when you call the getwritabledatabase () or getreadabledatabase () method of sqliteopenhelper to obtain the sqlitedatabase instance used to operate the database, if the database does not exist, the android system automatically generates a database, next, call the oncreate () method. The oncreate () method is called only when the database is generated for the first time () method to generate the database table structure and add the initialization data used by some applications. The onupgrade () method is called when the database version changes. The database version is controlled by the Program , assume that the current database version is 1 and the structure of the database table has been modified due to business needs. In this case, you need to upgrade the software and update the database table structure on your mobile phone, to achieve this goal, you can set the original database version to 2 (or another value) and update the table structure in the onupgrade () method. When the number of software version upgrades is large, you can use the onupgrade () method to determine based on the original number and target version number, and then update the table structure and data.

Both the getwritabledatabase () and getreadabledatabase () methods can obtain a sqlitedatabase instance used to operate the database. However, the getwritabledatabase () method opens the database in read/write mode. Once the disk space of the database is full, the database can only read but cannot write data. If the getwritabledatabase () method is used, an error occurs. The getreadabledatabase () method first opens the database in read/write mode. If the disk space of the database is full, it will fail to be opened. When the opening fails, it will continue to try to open the database in read-only mode.

 

For programmers familiar with SQL syntax, you can directly use execsql () and rawquery () to execute SQL statements to add, delete, update, and query data.

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.