Usage analysis of SQLite database for Android data persistence

This article describes the SQLite database usage of Android data persistence. Share with you for your reference, as follows:

In this section, I will summarize the knowledge of SQLite, another data store in Android

SQLite database comes with Android system. The main classes used include sqliteopenhelper and sqlitedatabase.

1. Sqliteopenhelper: an auxiliary class for creating database and database version management. This class is an abstract class, so we generally have a subclass sqliteopenhelper. The methods that need to be inherited and implemented mainly include oncreate(), onupgrade(), getwritabledatabase(). The getwritabledatabase () method returns the sqlitedatabase object instance. If the database has not been created, the oncreate () method will be called automatically to create the database. Therefore, some table creation and data initialization operations should be placed in the oncreate () method.

2. Sqlitedatabase: a class that operates SQLite database. It can perform SQL statements and add, delete, modify and query operations on the database. This object has encapsulated the basic database operations. You can call insert (), delete (), executesql () and other methods to perform actual database operations. This class is equivalent to connection in JDBC, session in Hibernate, or hibernatetemplate in spring; Transaction can also be controlled. Many operations on the database are finally called and executed through the sqlitedatabase instance.

Note: the database is private to an application, and the name of the database is unique in an application.

3. Corsor: cursor. Cursor enables random read-write access to the result set queried from the database. The query results of the database are generally returned by the subclass sqlitecursor.

Special note: during development, the first two classes are generally packaged, such as simple encapsulation, so that the query method of sqlitedatabase does not return the original cursor class (cursor is equivalent to resultset in JDBC), but returns business objects, etc

The implementation code is as follows:

Implementation of sqliteopenhelper class:

Implementation of SQLite class:

The above is the basic application of SQLite.

More readers interested in Android related content 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, summary of SD card operating methods of Android programming development, introduction and advanced tutorial of Android development , summary of Android resource operation skills, summary of Android view skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>