SQLite for Android Development (database)

Source: Internet
Author: User

SQLite database storage: SQLite is a lightweight relational database, it is very fast operation, the use of small resources, usually only need hundreds of K of memory is sufficient, and therefore particularly suitable for mobile devices.

First:

Create a database. (Android is designed to make it easier for us to manage databases, providing a Sqliteopenhelper helper class that makes it very simple to create and upgrade databases.) )

Sqliteopenhelper is an abstract class, which means that if we want to use it, we need to create our own helper class to inherit it. There are two abstract methods in Sqliteopenhelper, namely OnCreate ()

and Onupgrade (), we must rewrite the two methods in our own helper class, and then implement the logic of creating and upgrading the database separately in these two methods.

There are also two very important example methods in Sqliteopenhelper, Getreadabledatabase () and Getwritabledatabase (). Both of these methods can create or open an existing database, such as

If the database already exists, it opens directly or creates a new database) and returns an object that can read and write to the database. The difference is that when the database is not writable (such as disk space is full) getreadabledatabase ()

The object returned by the method will open the database in a read-only manner, and the Getwritabledatabase () method will appear with an exception.

There are two construction methods in Sqliteopenhelper that can be overridden, typically using a less-parametric construction method. This construction method receives four parameters, the first parameter is the Context, this has nothing to say, must have it to

Can operate on a database. The second parameter is the database name, which is the name specified here when the database is created. The third parameter allows us to return a custom Cursor when querying the data, usually passing null. The fourth parameter of the

Number represents the version number of the current database and can be used to perform an upgrade operation on the database. After building an instance of Sqliteopenhelper, calling its getreadabledatabase () or Getwritabledatabase () method can create a number

According to the library, the database files will be stored in the/data/data/<package name>/databases/directory. At this point, the overridden OnCreate () method is also executed, so there is usually some logic to create the table here.

SQLite does not have many miscellaneous data types like other databases, its data types are simple, integer denotes integer, real is floating point, text is type, and blob represents binary type.

SQLite for Android Development (database)

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.