Android SQLite database creation and usage (2)

Source: Internet
Author: User
Tags sql tutorial

This article will not be practical. Some resources are quite good. Let's share a link with you. The online cool predecessors can always create benefits for our elders !!! Young !!! Haha! I have found a good website on the Internet, which is better than the textbooks. I would like to share it with everyone. PS: Thank you !!

SQL Tutorial: W3School

At the same time, I attached a note photo which I started learning SQL statements in the Early Learning Mode (ps: I was not born in a computer science class, I have learned these things, and object-oriented work also needs to be learned in the company. ).


Before basic operations on the Android database, create and use the Android SQLite database. (1) the source code has been attached to the blog, and the resources also have projects. Not much explanation. It is a standard template. Next, let's talk about these classes in Android.

Let's take a look at the SQLiteOpenHelper class, for example, from the official website:



Next, let's take a look at SQLiteDatabase, which is also from the official website:


You cannot find the method by yourself. The main construction is described below:

The figure shows some properties of the class. As follows:

Use SQLiteOpenHelper to manage database versions

If the application uses the SQLite database, we must create the database table structure used by the application and add some initialization records when the user first uses the software. In addition, when the software is upgraded, you also need to update the data table structure. How does one automatically create the database table required by the application on the user's mobile phone when the user first uses or upgrades the software? In the Android system, an abstract class named SQLiteOpenHelper is provided, which must be inherited before it can be used. It manages the database version to meet the preceding requirements. To manage database versions, the SQLiteOpenHelper class provides two important methods: onCreate (SQLiteDatabase db) and onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ), the former is used to generate a database table when you first use the software, and the latter is used to update the database table structure when you upgrade the software. When the getWritableDatabase () or getReadableDatabase () method of SQLiteOpenHelper is called to obtain the SQLiteDatabase instance used to operate the database, if the database does not exist, the Android system automatically generates a database and then calls onCreate () method. The onCreate () method is called only when the database is generated for the first time. In the onCreate () method, you can generate the database table structure and add the initialization data used by some applications. The onUpgrade () method is called when the database version changes. Generally, the version number needs to be changed during software upgrade. The database version is controlled by programmers, assume that the current database version is 1 and the database table structure is modified due to business changes. In this case, you need to upgrade the software to update the database table structure on your mobile phone, to achieve this goal, you can set the original database version to 2 and update the table structure in the onUpgrade () method. When the number of software version upgrades is large, you can use the onUpgrade () method to determine based on the original number and target version number, and then update the table structure and data. Both the getWritableDatabase () and getReadableDatabase () methods can obtain a SQLiteDatabase instance used to operate the database. However, the getWritableDatabase () method opens the database in read/write mode. Once the disk space of the database is full, the database can only read but cannot write. If getWritableDatabase () is used to open the database, an error occurs. The getReadableDatabase () method first opens the database in read/write mode. If the disk space of the database is full, it will fail to be opened. When the opening fails, it will continue to attempt to open the database in read-only mode.

Use SQLiteDatabase to operate SQLite Databases

This class encapsulates APIs for database operations. You can use this class to Create, query, Update, and Delete data) operation (CRUD for short ). For SQLiteDatabase learning, we should master the execSQL () and rawQuery () methods. The execSQL () method can be used to execute SQL statements with changing behaviors such as insert, delete, update, and CREATE TABLE. The rawQuery () method is used to execute select statements.

PS: I have no explanation for the Cursor class. I think about it myself. The prompt is similar to Map in the key-value format.




Related Article

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.