SQLiteOpenHelper

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.

This class makes it easy for ContentProvider implementations to defer opening and upgrading the database until first use, to avoid blocking application startup with long-running database upgrades.

For an example, see the NotePadProvider class in the NotePad sample application, in the samples/ directory of the SDK.

Note: this class assumes monotonically increasing version numbers for upgrades.

理解:

1.一个用来管理数据库的创建和版本升级的辅助类

2.提供了onCreate()和onUpgrade()两个回调函数,允许我们再创建和升级数据库,进行自己的操作(保证数据库的唯一性,如果数据库存在,则不会重新创建)

3.onOpen()在每次打开数据库的时候会被调用

3.ContentProvider(如ContentValues)传递数据,方便使用

 

Public methods

(1)close ----void close ()

Close any open database object.

(2)getDatabaseName----String getDatabaseName ()

Return the name of the SQLite database being opened, as given to the constructor.

(3)getReadableDatabase----SQLiteDatabase getReadableDatabase ()

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.

(4)getWritableDatabase----SQLiteDatabase getWritableDatabase ()

Create and/or open a database that will be used for reading and writing. The first time this is called, the database will be opened andonCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and/or onOpen(SQLiteDatabase) will be called.

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 the database.) Errors such as bad permissions or a full disk may cause this method to fail, but future attempts may succeed if the problem is fixed.

Database upgrade may take a long time, you should not call this method from the application main thread, including from ContentProvider.onCreate().

(5)onConfigure----void onConfigure (SQLiteDatabase db)(配置)

Called when the database connection is being configured, to enable features such as write-ahead logging or foreign key support.

This method is called before onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int), onDowngrade(SQLiteDatabase, int, int), or onOpen(SQLiteDatabase) are called. It should not modify the database except to configure the database connection as required.

This method should only call methods that configure the parameters of the database connection, such as enableWriteAheadLogging()setForeignKeyConstraintsEnabled(boolean), setLocale(Locale), setMaximumSize(long), or executing PRAGMA statements.

(6)onCreate----void onCreate (SQLiteDatabase db)

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.

(7)onDowngrade----void onDowngrade (SQLiteDatabase db, int oldVersion, int newVersion)(降级)

Called when the database needs to be downgraded. This is strictly similar to onUpgrade(SQLiteDatabase, int, int) method, but is called whenever current version is newer than requested one. However, this method is not abstract, so it is not mandatory for a customer to implement it. If not overridden, default implementation will reject downgrade and throws SQLiteException

This method executes within a transaction. If an exception is thrown, all changes will automatically be rolled back.

(8)onOpen----void onOpen (SQLiteDatabase db)

Called when the database has been opened. The implementation should check isReadOnly() before updating the database.

This method is called after the database connection has been configured and after the database schema has been created, upgraded or downgraded as necessary. If the database connection must be configured in some way before the schema is created, upgraded, or downgraded, do it in onConfigure(SQLiteDatabase) instead.

(9)onUpgrade----void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)

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.

This method executes within a transaction. If an exception is thrown, all changes will automatically be rolled back.

(10)setWriteAheadLoggingEnabled----void setWriteAheadLoggingEnabled (boolean enabled)

Enables or disables the use of write-ahead logging for the database. Write-ahead logging cannot be used with read-only databases so the value of this flag is ignored if the database is opened read-only.

理解:

SQLiteOpenHelper 类的基本用法是:当需要创建或打开一个数据库并获得数据库对象时,首先根据指定的文件名创建一个辅助对象,然后调用该对象的getWritableDatabase 或 getReadableDatabase方法 获得SQLiteDatabase 对象。 
调用getReadableDatabase 方法返回的并不总是只读数据库对象,一般来说该方法和getWriteableDatabase 方法的返回情况相同,只有在数据库仅开放只读权限或磁盘已满时才会返回一个只读的数据库对象。

 

posted on 2017-03-13 14:32  不二家的瓶子  阅读(610)  评论(0编辑  收藏  举报

导航