Android SQLite database details

Source: Internet
Author: User
Android SQLite database details when writing database application software, we need to consider this problem: because the software we developed may be installed on the mobile phones of many users, if the application uses the SQLite database, we must create the database table structure used by the application and add some initialization records when the user first uses the software. In addition, when the software is upgraded, you also need to update the data table structure. Then, how can we automatically create the database tables required by the application on the user's mobile phone when the user first uses or upgrades the software? We cannot manually create database tables on every mobile phone that requires the software to be installed? This requirement is required for every database application. Therefore, in the Android system, an abstract class named sqliteopenhelper is provided, which must be inherited before it can be used, it manages the database version to meet the previous requirements. Next, let's take a look at the most common classes and methods for Android SQLite: 1. sqliteopenhelper: oncreate (sqlitedatabase dB); used to generate database tables when you use software for the first time. When the getwritabledatabase () or getreadabledatabase () method of sqliteopenhelper is called to obtain the sqlitedatabase instance used to operate the database, if the database does not exist, the android system automatically generates a database and then calls oncreate () method: The oncreate () method is called only when the database is generated for the first time. In the oncreate () method, you can generate the database table structure and add the initialization data onupgrade (sqlitedatabase dB,  Int Oldversion, Int Newversion); it is called when the database version changes. Generally, the getwritabledatabase (); getreadabledatabase () version must be changed during software upgrade (); both 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. If getwritabledatabase () is used to open the database, 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 attempt to open the database in read-only mode. Ii. sqlitedatabase: 1 , DB. insert (table, nullcolumnhack, values) the first parameter is the table name, and the second parameter is the default value of an empty column, the third parameter is a map of the contentvalues type that encapsulates the column name and column value; 2 DB. Delete (table, whereclause, whereargs) the first parameter is the table name, the second parameter is the deletion condition, and the third parameter is the deletion condition value array. 3 , DB. update (table, values, whereclause, whereargs) the first parameter is the table name, and the second parameter is a map of the contentvalues type in the row and column ), the third parameter is the update condition (where clause), and the fourth parameter is the update condition array. 4, DB. Query (table, columns, selection, selectionargs, groupby, having, orderby) (The following describes the method in detail) 5、db.exe csql (SQL) //  Execute any SQL statement 6 , DB. rawquery (SQL, selectionargs) describes the meaning of each parameter in the fourth method: cursor query (table, columns, selection, selectionargs, groupby, having, orderby, limit: ① table: Table Name ② columns: column name array ③ Selection: condition, equivalent to where ④ selectionargs: condition, parameter array ⑤ groupby: group column ⑥ having: group condition 7orderby: sort Column Limit limit: Paging query limit 3. cursor: cursor is a cursor interface that provides methods to traverse the query results, such as moving the pointer to move (), the getstring () method to obtain the column value. cursor cursor common method: getcount () Total number of records isfirst () Determine whether the first record islast () Determine whether the last record movetofirst () moves to the first record movetolast () move to the last record move (  Int Offset) to move the specified record movetonext () to the next record movetoprevious () to the previous record getcolumnindexorthrow (string columnname) to get the column index getint (  Int  Columnindex) to get the int type value of the specified column index getstring (  Int Columnindex) to obtain the string type value of the specified column.

 

Related Article

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.