Chapter 5 data-centric-Data Access (4), Chapter 5 Access

Source: Internet
Author: User

Chapter 5 data-centric-Data Access (4), Chapter 5 Access
5.2 common data operations-Databases

When it comes to data access, databases must be a common solution. Android also has its own database. Let's take a look at the differences between the Android database and the general database.

5.2.1SQLite database Introduction

Currently, SQLite3 is integrated in the Android system. It supports SQL statements and is a lightweight embedded database. SQLite supports NULL, INTEGER, REAL, TEXT, and BLOB data types. Instead of static data types, SQLite uses column relationships. We can regard the SQLite database as a database without data types, you can store any type of data in a field other than a non-Integer primary key (the length of the field is unlimited ). However, we recommend that you follow the standard SQL syntax when writing SQL statements, because it is easier for others to understand your code.

The official website of SQLite is htt: // www.sqlite.org/. you can access this website to learn more about sqlite.

In Android development, a SQLiteDatabase instance represents an SQLite database. Through some methods of the SQLiteDatabase instance, we can execute SQL statements, adds, deletes, queries, and modifies databases. Note that the database is private to an application, and the database name is unique in an application. The following sections describe database operations in detail.

 

Experience Sharing:

The database is stored in data/<project folder>/databases /. Sometimes we may need to view the content in the database. In this case, we can copy it and use the database tool to view it.

In addition, files are used as a storage carrier for most of the time, but in some cases, there may be problems with file storage. Pay special attention to the following:

1) multi-threaded data access is related.

2) If the application processes complex data structures that may change.

 

5.2.2 create and open a database

To operate a database, you must first open a database. here you need to use a class: android. database. sqlite. SQLiteOpenHelper. It encapsulates how to open a database. Of course, it also contains the logic for creating a database if the database does not exist.

// Import omitted

Public class DBOpenHelper extends SQLiteOpenHelper {

 

Public static final String DATABASE_NAME = "myDataBaseName ";

Public static final int DATABASE_VERSION = 1;

Public static final String TABLE_NAME = "myTableName ";

 

Public DBOpenHelper (Context context ){

Super (context, DATABASE_NAME, null, DATABASE_VERSION );

}

@ Override

Public void onCreate (SQLiteDatabase db ){

Db.exe cSQL ("create table" + TABLE_NAME

+ "(_ Id integer primary key autoincrement, name text );");

}

@ Override

Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){

Db.exe cSQL ("drop table if exists notes ");

OnCreate (db );

}

}

---------------------------------------- It is difficult for programmers to make money. Therefore, they must learn financial management. The annual ROI of the p2p platform affiliated to Ping An group is 7%-9%. This is the preferred personal experience to replace bank financial management. We recommend investing in don't invest in security, which is almost impossible to transfer. It is very difficult to withdraw the website link in advance. Don't make it in white --------------------------------------------

The following describes the above Code.

OnCreate (SQLiteDatabase): This method is called when the database is generated for the first time. Generally, the database table is generated in this method.

OnUpgrade (SQLiteDatabase, int, int): When the database needs to be upgraded, the Android system will actively call this method. In general, we delete a data table in this method and create a new data table. Of course, whether other operations are required depends on the needs of the application.

In addition to the above two methods, onOpen (SQLiteDatabase) may also be used, which is the callback function when the database is opened.

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.