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.