Android SQLite Database Basics (1)

Source: Internet
Author: User

Sqliteopenhelper class: is an abstract class that provides operational functions such as database opening and closing by implementing user classes from this class of inheritance.

Sqlitedatabase Class: Database Access class: Perform operations such as inserting records into the database, querying records, and so on.

Sqlitecursor class: Query structure Action class: Used to access records in query results.

This section first discusses the next Sqliteopenhelper class.

1. The method that must be implemented is: "OnCreate ()" is used to create data when it is called, generally used to do initialization work.

The "OnUpdate ()" method is used to update the database.

2. Need to complete super

3, Sqliteopenhelper provides methods Getreadabledatabase () and Getwriteabledatabase () methods to obtain a Sqlitedatabase object, where the object can allow access to the database, Regardless of whether it is in 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 construction method, used to define database name, database query result set, database version
* Super The method of the parent class
* @param context: for contexts
*/
Public Sqlite_persondb (Context context) {
Super (context, "lin_person.db", null,1);
}
/**
* SQLite Database construction method, used to define database name, database query result set, database version
* @param context passed in contextual parameters
* @param name incoming database names
* @param factory incoming cursor mode, NULL is the default format
* @param version of incoming database, requires more than 1, otherwise it will error
*/
Public Sqlite_persondb (Context context, String name, Cursorfactory factory,
int version) {
Super (context, name, Factory, version);
}
/**
* Method of creating a database call for the first time
* Incoming DB is the created database, which is assumed to be lin_person.db
* Execsql CREATE database table contents, ignoring case.
*/
@Override
public void OnCreate (Sqlitedatabase db) {
/**
* Syntax: CREATE TABLE table name (column name type set primary KEY (optional) Set auto-grow (optional));
* @param primary key to set the primary key
* @param autoincrement automatic growth
*/
Db.execsql ("CREATE TABLE person (ID integer PRIMARY key autoincrement,name varchar (), phonenum varchar (11)");
}

/**
* Upgrade Database
*/

@Override
public void Onupgrade (sqlitedatabase arg0, int arg1, int arg2) {
Temporarily ignore
}

}

Android SQLite Database Basics (1)

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.