Basic concepts of Android databases (I) and basic concepts of android

Source: Internet
Author: User

Basic concepts of Android databases (I) and basic concepts of android

1. Introduction to the Android Database

Android uses the SQLite database and Content Provider in combination to provide the persistent function of structured data.

SQLite databases can store application data in a structured and easy-to-manage way. Android provides a complete library file for SQLite relational databases. Each application can create its own database and have full control over the database.

The Android database is stored in the/data/<package name>/database folder on the device. All databases are private and can only be accessed by Applications created for them.

2. Introduction to Content Provider

Content Provider provides a simple URI addressing model based on the "content: //" mode to publish and use data. It is one of the four major Android components.

After the underlying data storage is created, you can use the Content Provider, which performs consistent abstraction through the underlying data source, so that the application does not have to rely too much on a data source.

It can be used to share data between applications. For example, our applications can retrieve text messages or contacts. Of course, we can also release our own Content Provider so that others can perform CRUD operations on our database in their applications.

Specific usage methods will be introduced in later topics.

3. SQLite Introduction

SQLite is a popular Relational Database management System ). Features: open-source compliant with standard and lightweight. It has been implemented as a concise C language and has become part of the Android software stack.

It is implemented as a database instead of being executed as an independent process. Each SQLite database is a complete Part of the application that creates it. This reduces the external dependencies of applications, minimizes latency, and simplifies transaction locking and synchronization.

SQLite is very reliable and is the preferred Database System for many consumer electronic products (such as many MP3 players and smartphones.

4. Content Value and Cursor

Content Value is used to Insert a new row into a table in the database. Each ContentValues object represents a table row as a column name and maps it To the value.

Database query is returned as a Cursor object. Cursor is a pointer to the result set in the underlying data. It does not extract or return a copy of the result.

Common Cursor methods:

Cursor. moveToFirst (); // move the cursor to the first row of cursor in the query result. moveToNext (); // move the cursor to the next row of cursor. moveToLast (); // move the cursor to the last row of cursor. moveToPrevious (); // move the cursor forward to a row of cursor. getCount (); // get the number of result sets cursor. getColumnIndexOrThrow (""); // returns its index based on the specified column name, starting from 0. If no value exists, an exception cursor is thrown. getColumnNames (); // obtain the string data of all column names in the current cursor. getColumnName (1); // return the column name cursor Based on the index. moveToPosition (1); // move the cursor to the specified row cursor. getPosition (); // get the position of the current cursor
5. SQLiteOpenHelper Introduction

SQLiteOpenHelper is an abstract class used to create, open, and upgrade databases.

SQLiteOpenHelper caches the database instances after they are successfully opened. Therefore, we can request to open the database before executing a query or transaction, unless we no longer need to use the database, we do not need to manually close them.
Database operations, especially the opening and creation operations, take a long time to complete. Therefore, to ensure that these operations do not affect the user experience, the database operations should be executed asynchronously.

Generally, in our application, we need to define a class that inherits the SQLiteOpenhelper class, and rewrite its constructor onCreate onUpgrade method to process the creation of databases and upgrade them to the new version of databases respectively.


Some basic concepts of databases are briefly introduced here. The next practical Code introduces the addition, deletion, modification, and query operations of android databases.


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.