Android SQLite database BASICS (1), androidsqlite

Source: Internet
Author: User

Android SQLite database BASICS (1), androidsqlite

SQLiteOpenHelper class: it is an abstract class that inherits from this class to Implement User classes and provides operation functions such as opening and closing databases.

SQLiteDatabase class: Database Audit class: Execute insert records and query records on the database.

SQLiteCursor class: Query structure operation class: used to access records in query results.

This section describes the SQLiteOpenHelper class.

1. The required method is "onCreate ()", which is used for data creation and initialization.

The onUpdate () method is used to update the database.

2. super needs to be completed

3. SQLiteOpenHelper provides the getReadableDatabase () and getWriteableDatabase () methods to obtain a SQLiteDatabase object. The object can allow access to the database, regardless of the read or write mode.

 

 

 

Code:

Package com. example. sqllites. dbs;

Import android. content. Context;
Import android. database. sqlite. SQLiteDatabase;
Import android. database. sqlite. SQLiteDatabase. CursorFactory;
Import android. database. sqlite. SQLiteOpenHelper;
/**
*
* @ Author Lin
* SQLite help class
*/
Public class sqlite_Persondb extends SQLiteOpenHelper {

/**
* Sqlite database constructor is used to define the database name, database query result set, and database version.
* Super: the method of the parent class
* @ Param context: the context.
*/
Public sqlite_Persondb (Context context ){
Super (context, "lin_Person.db", null, 1 );
}
/**
* Sqlite database constructor is used to define the database name, database query result set, and database version.
* @ Param context incoming context parameters
* @ Param name: Enter the Database name
* @ Param factory: The cursor type passed in. null is the default format.
* @ Param version: the database version must be 1 or later. Otherwise, an error is returned.
*/
Public sqlite_Persondb (Context context, String name, CursorFactory factory,
Int version ){
Super (context, name, factory, version );
}
/**
* Method for the first database creation call
* The input db is the created database, which is assumed to be lin_Person.db
* ExecSQL creates database table content, regardless of case.
*/
@ Override
Public void onCreate (SQLiteDatabase db ){
/**
* Syntax: create table Name (column name type setting primary key (optional) Setting Automatic growth (optional ));
* @ Param primary key: Set the primary key
* @ Param autoincrement automatic Growth
*/
Db.exe cSQL ("create table person (id integer primary key autoincrement, name varchar (20), phoneNum varchar (11 ))");
}

/**
* Upgrade the database
*/

@ Override
Public void onUpgrade (SQLiteDatabase arg0, int arg1, int arg2 ){
// Temporarily ignore
}

}

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.