Activity 4-Part2-SQLite

Source: Internet
Author: User

The last time we talked about the File operation in part1, let's take a look at the SQLite-related content. First, let's take a look at one:

I believe everyone who has read part1 knows the meaning of this. The method for opening or creating a database, and the returned value is a class of SQLiteDatabase, which will be explained in detail; let's talk about the parameters of this method and look at the figure below:

 

The first parameter is the database name, the second is the operation mode, and the third parameter is an optional factory class. When performing the query operation, a cursor subclass is instantiated, if null is used, the default factory is used.

We know that SQLIiteDatabase returned by this method will be used for subsequent database operations. Let's take a look at the following SQLiteDatabase class. Before that, we will know through the official documentation that it is included in

Android. datebase. sqlite:

This package contains the QLite database management class and can be used to manage your own private database. Applications can use these classes to manage private databases. If you create a Content Provider, you may have to use these classes to create and manage your own databases to store Content. You can view the benefits of using Content Providers to learn how to implement a content provider. You can view the NotePadProvider class of the NotePad sample program in the SDK as an example to learn the content provider. Android is equipped with SQLite 3.4.0.
If you are processing data from a provider, you will not use these SQLite classes, but use the general android. database Class instead.
Android released a sqlite3 database tool, which is located in the tools directory of the SDK. You can enter sqlite3 on the terminal to use this tool to browse devices or use SQL commands.


Let's take a look at the content contained in this package:

There are the following interfaces:

 

There are the following classes:

The focus is on the above SQLiteCursor class and SQLiteDatabae class, as well as a more concise class SQLiteOpenHelper class. Of course there is also the second above interface SQLiteDatebase. CursorFactory, which is the second parameter of the method just mentioned above.

SQLiteDatabase commonly used methods are nothing more than opening or creating a database, querying, inserting example sentences, etc. But another important method is execSQL (String), which allows us to execute SQL commands, andeoid provides an easy way to operate databases, but also allows you to use SQL statements.

Here is an example of each of the two methods. I believe you are familiar with it. For example, we want to insert a record in a database:

Method 1: Use the insert method of SQLiteDatabase: www.2cto.com


Private void insert (SQLiteDatabase db ){
ContentValues conval = new ContentValues ();
Conval. put ("first_field", value );
Db. insert (table, null, conval );
}
Private void insert (SQLiteDatabase db ){
ContentValues conval = new ContentValues ();
Conval. put ("first_field", value );
Db. insert (table, null, conval );
} In this way, a record is inserted into the table of the db database, and the value of the first field of the record is value;

Method 2: execSQL:

First, construct such an SQL statement and use it as the execSQL () parameter to execute the function:


Private void insert (SQLiteDatabase db ){
String SQL = "insert into table (first_field) values (" value ")";
Db.exe cSQL (SQL );
}
Private void insert (SQLiteDatabase db ){
String SQL = "insert into table (first_field) values (" value ")";
Db.exe cSQL (SQL );
} Some other operations are similar and will not be listed one by one.

The next step is the SQLiteOpenHelper class, which is a help class for SQLiteDatabase to manage database creation and version updates. The general usage is to define a class to inherit it and implement its two Abstract METHODS onCreate () and onUpgrade (). For example:


Class ExampleHelper extends SQLiteOpenHelper {
Private static final String CREATE = "create table example (id integer, second text )";

Private SQLiteDatabase db;

@ Override
Public void onCreate (SQLiteDatabase db ){
Db.exe cSQL (CREATE );
}

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

}
}

From the column chenlong12580

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.