public abstract class

SQLiteOpenHelper

extends Object

Class Overview

A helper class to manage database creation and version management. You create a subclass implementing onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and optionally onOpen(SQLiteDatabase), and this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary. Transactions are used to make sure the database is always in a sensible state.

helper类主要用于管理数据库的创建和版本管理。可以实现它的一些函数,如:onCreate()/onUpdate/onOpen()。

下面是一些常用的成员函数:

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

Since: API Level 1

Create a helper object to create, open, and/or manage a database. The database is not actually created or opened until one of getWritableDatabase() or getReadableDatabase() is called.

Parameters
context to use to open or create the database
name of the database file, or null for an in-memory database
factory to use for creating cursor objects, or null for the default
version number of the database (starting at 1); if the database is older, onUpgrade(SQLiteDatabase, int, int) will be used to upgrade the database

public synchronized SQLiteDatabase getReadableDatabase ()

Since: API Level 1

Create and/or open a database. This will be the same object returned by getWritableDatabase() unless some problem, such as a full disk, requires the database to be opened read-only. In that case, a read-only database object will be returned. If the problem is fixed, a future call to getWritableDatabase() may succeed, in which case the read-only database object will be closed and the read/write object will be returned in the future.

Returns
  • a database object valid until getWritableDatabase() or close() is called.
Throws
SQLiteException if the database cannot be opened

public synchronized SQLiteDatabase getWritableDatabase ()

Since: API Level 1

Create and/or open a database that will be used for reading and writing. Once opened successfully, the database is cached, so you can call this method every time you need to write to the database. Make sure to call close() when you no longer need it.

Errors such as bad permissions or a full disk may cause this operation to fail, but future attempts may succeed if the problem is fixed.

Returns
  • a read/write database object valid until close() is called
Throws
SQLiteException if the database cannot be opened for writing

public abstract void onCreate (SQLiteDatabase db)

Since: API Level 1

Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.

Parameters
db The database.

这个函数在第一次创建数据库时被调用。

public abstract void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)

Since: API Level 1

Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.

The SQLite ALTER TABLE documentation can be found here. If you add new columns you can use ALTER TABLE to insert them into a live table. If you rename or remove columns you can use ALTER TABLE to rename the old table, then create the new table and then populate the new table with the contents of the old table.

Parameters
db The database.
oldVersion The old database version.
newVersion The new database version.

实际编程经验:可以定义一个继承于SQliteOpenHelper的helper子类,在这个子类中完成数据库的建立,更新,以及修改、查询等操作。

更多相关文章

  1. C语言函数以及函数的使用
  2. android sqilte3数据库大小的测试
  3. android执行外部程序,类似DELPHI里的EXEC函数
  4. Android 取出 图片以字节形式存放到数据库的数据,并将图片显示
  5. android 建数据库 SQLite 存储sd 卡或者内存
  6. android 数据库 备份还原
  7. Android SQLiteOpenHelper(手机数据库)
  8. [Android Studio] Android Studio如何提示函数用法
  9. Android中有关数据库SQLite的介绍

随机推荐

  1. Android选项卡Tab的实现
  2. android修改手机默认音量配置
  3. 【Android】Android(安卓)彩信发送的两种
  4. Android资料链接保存
  5. [Android(安卓)UI界面] android中仿iphon
  6. android定时器
  7. Android倒计时 Android仿京东倒计时 Andr
  8. Android(安卓)USB Storage原理(1)
  9. [置顶] android常用网址收集
  10. Android之ScrollView嵌套ListView