Android Chinese API (139) -- SQLiteOpenHelper

Source: Internet
Author: User

 

Preface

This chapter content android. database. sqlite. SQLiteOpenHelper chapter, version for Android 4.0 r1, translated from: "StreamH", welcome to his blog: "http://blog.csdn.net/qs_csu", thank you again "StreamH "! We look forward to your participation in translation of Android related information, contact me over140@gmail.com.

 

Statement

You are welcome to repost, but please keep the original source of the article :)

Blog Garden: http://www.cnblogs.com/

Android Chinese translation group: http://androidbox.sinaapp.com/

 

 

SQLiteOpenHelper

Translator's signature: StreamH

Link: http://blog.csdn.net/qs_csu

Version: Android 4.0 r1

 

Structure

Inheritance relationship

Public abstract class SQLiteOpenHelper extends Object

Java. lang. Object

Android. database. sqlite. SQLiteOpenHelper

 

Class Overview

A help class that helps you create databases and manage database versions.

You must create a subclass to implement the onCreate (SQLiteDatabase), onUpgrade (SQLiteDatabase, int, int) methods, and any onOpen (SQLiteDatabase) method, at the same time, you must ensure that the database exists when you open the database. If the database does not exist, create it and perform necessary upgrades to maintain the best state.

It is very easy to use the content provided by this class to create a database. First, you must upgrade the database to avoid blocking data after the database is started for a long time.

For example, in the NotePad example program, see NotePadProvider class, under the SDK directory samples.

Note:: This class assumes that the version number of the upgrade is monotonically increasing.

 

Constructor

PublicSQLiteOpenHelper(Context context, String name, SQLiteDatabase. CursorFactory factory, int version)

Create a help object to open or manage the database. This method usually returns quickly. The database is not actually created or opened until one of getWritableDatabase () or getReadableDatabase () is called.

Parameters

Context is used to open or create a database.

Name database file name, which is null for a database in memory

The factory is used to create an object cursor, or the default value is null.

Version database sequence number (starting from 1); if the database is old, onUpgrade (SQLiteDataBase, int, int) will be called to upgrade the database; if the database is new, onDowngrade (SQLiteDatabase, int, int) will be called to downgrade the database.

 

PublicSQLiteOpenHelper(Context context, String name, SQLiteDatabase. CursorFactory factory, int version, DatabaseErrorHandler errorHandler)

Create a help object to open or manage the database. This method usually returns quickly. The database is not actually created or opened until one of getWritableDatabase () or getReadableDatabase () is called.

Parameters

Context is used to open or create a database.

Name database file name, which is null for a database in memory

The factory is used to create an object cursor, or the default value is null.

Version database sequence number (starting from 1); if the database is old, onUpgrade (SQLiteDataBase, int, int) will be called to upgrade the database; if the database is new, onDowngrade (SQLiteDatabase, int, int) will be called to downgrade the database.

ErrorHandler when sqlite reports a database destruction error, DatabaseErrorHandler will be used.

 

Common Methods

Public synchronized voidClose()

Close any opened database objects.

 

Public StringGetDatabaseName()

Returns the name of the SQLite database that is being opened through the constructor.

 

Public synchronized SQLiteDatabaseGetReadableDatabase()

Create or open a database. This is the same as the object returned by getWritableDatabase (), unless some factors require that the database be opened only in read-only mode, for example, the disk is full. In this case, a read-only database object will be returned. If this problem is modified, getWritableDatabase () may be successfully called in the future, and the read-only database object will be closed and the read-write object will be returned.

Like getWritableDatabase (), This method takes a long time to return, so you should not call it in the main thread of the application, including the method ContentProvider. onCreate ().

Return Value

A valid database object until getWritableDatabase () or close () is called.

Exception

SQLiteException if the database cannot be opened

 

Public synchronized SQLiteDatabaseGetWritableDatabase()

Create or open a database for reading and writing. When this method is called for the first time, the database is opened, and onCreate (SQLiteDatabase), onUpgrade (SQLiteDatabase, int, int), or onOpen (SQLiteDatabase) will be called.

Once it is successfully opened, the database will be cached, so you can call this method when writing data. (When you no longer need this database, make sure to call close ()). Errors such as unhealthy licenses or full disk may cause this method to fail. However, if the problem is resolved, future attempts may succeed.

Database Upgrade may take a long time, so you shouldn't call it in the main thread of the application, including the ContentProvider. onCreate () method ().

Return Value

A valid read/write database object until close () is called.

Exception

SQLiteException if the database cannot be opened for writing

Public abstract voidOnCreate(SQLiteDatabase db)

Called when a database is created for the first time. The number of tables created and initialized is completed here.

Parameters

Db database

 

Public voidOnDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)

Called when the database needs to be downgraded. This method is similar to onUpgrade (), but it will be called as long as the version is later than the requested update. However, this method is not abstract, so it does not force users to implement it. If it is not overwritten, the default implementation rejects downgrading and throws a SQLiteException.

Parameters

Db database

OldVersionThe old version Database

NewVersionThe new database version

 

Public voidOnOpen(SQLiteDatabase db)

Called when the database is opened. This implementation checks isReadOnly () Before upgrading the database ().

Parameters

Db database

 

Public abstract voidOnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

Called when the database is upgraded. This method is used to discard tables, add tables, or do other things. It needs to be upgraded to the version in the new plan.

The SQLite alter table document can be found here. If you add new columns, you can use alter table to insert them into a living TABLE. If you rename or move columns, you can use alter table to rename these old tables, create a new TABLE, and then place the data in the old TABLE to the new TABLE.

Parameters

Db database

OldVersionThe old version Database

NewVersionThe new database version

 

Supplement

Article Selection

Android SQLiteOpenHelper example

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.