Detailed explanation of SQLite database operation method for Android programming
This paper describes the operation method of Android SQLite database. Share with you for your reference, as follows:
sqlite and Android
Introduction to SQLite
SQLite is a very popular embedded database. It supports SQL language and has good performance with little memory. In addition, it is open source and anyone can use it.
SQLite consists of the following components: SQL compiler, kernel, backend, and attachments. SQLite makes it more convenient to debug, modify and expand the kernel of SQLite by using virtual machine and virtual database engine (vdbe).
The data types supported by SQLite include:
1. Text (string similar to Java) 2. Integer (long similar to Java) 3. Real (double similar to Java)
For more knowledge of SQLite data types, please refer to the previous related articles: explain the data types in SQLite in detail
sqlite In Android
Android integrates SQLite at runtime. Therefore, using SQLite database in Android does not require the installation process and obtaining database permissions. You only need to define the statements to create and update the database, and the rest will be handled by the Android platform for you.
Operating SQLite database usually means operating the file system, which is still time-consuming. Therefore, it is recommended to execute database operations asynchronously.
Your application creates a SQLite database. By default, the data is stored in / data / data / APP_ NAME/databases/FILENAME。 Here, data is the value returned by the environment. Getdatadirectory () method, app_ Name is the name of your app package
Using SQLite database in Android Development
Activities can access a database using content provider or service.
Create database
Android does not automatically provide a database. To use SQLite in Android applications, you must create your own database, and then create tables, indexes and fill in data. Android provides a sqliteopenhelper to help you create a database. As long as you inherit the sqliteopenhelper class, you can easily create a database.
The sqliteopenhelper class encapsulates the logic used to create and update the database according to the needs of developing applications. The subclass of sqliteopenhelper needs to implement at least three methods:
Constructor to call the constructor of the parent class sqliteopenhelper. This method requires four parameters: context, database name, an optional cursor factory (usually null), and an integer representing the version of the database model you are using. Oncreate () method, which requires an sqlitedatabase object as a parameter, and fills the table and initializes the data for this object as needed. Onupgrade () method, which requires three parameters, an sqlitedatabase object, an old version number and a new version number, so that you can know how to change a database from an old model to a new model.
First, define the table structure to be created and use classes for abstraction. Here is an example to define a Sina meager account class:
The following code shows how to inherit sqliteopenhelper to create a database. The singleton class is recommended:
Add, delete, modify and query database
Because SQLite supports standard SQL statements, we can use standard SQL statements to add, delete, modify and query the database. It is recommended to use placeholder SQL statements, which looks more refreshing. Here is my code example:
Transaction (dbtransaction)
The database cache is often used in Android, especially in the case of WiFi. In case of data inconsistency, the cached data needs to be updated. At this time, transaction processing needs to be used to ensure the integrity and speed of operation. An example of using SQLite to ensure transaction integrity in Android is as follows:
Start the transaction through begintransaction(), end the transaction with endtransaction(), and set the transaction execution success id settransactionsuccessful()
For more Android related content, readers who are interested can view the special topics of this site: summary of Android operating SQLite database skills, summary of Android database operating skills, summary of activity operating skills of Android programming, summary of Android file operating skills, introduction and advanced tutorial of Android development, summary of Android resource operating skills Android view skills summary and Android control usage summary
I hope this article will help you in Android programming.