public abstract class SQLiteOpenHelper
extends java.lang.Object
onCreate(net.sqlcipher.database.SQLiteDatabase)
, onUpgrade(net.sqlcipher.database.SQLiteDatabase, int, int)
and
optionally onOpen(net.sqlcipher.database.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.
For an example, see the NotePadProvider class in the NotePad sample application, in the samples/ directory of the SDK.
Constructor and Description |
---|
SQLiteOpenHelper(Context context,
java.lang.String name,
SQLiteDatabase.CursorFactory factory,
int version)
Create a helper object to create, open, and/or manage a database.
|
SQLiteOpenHelper(Context context,
java.lang.String name,
SQLiteDatabase.CursorFactory factory,
int version,
SQLiteDatabaseHook hook)
Create a helper object to create, open, and/or manage a database.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close any open database object.
|
SQLiteDatabase |
getReadableDatabase(char[] password) |
SQLiteDatabase |
getReadableDatabase(java.lang.String password)
Create and/or open a database.
|
SQLiteDatabase |
getWritableDatabase(char[] password) |
SQLiteDatabase |
getWritableDatabase(java.lang.String password)
Create and/or open a database that will be used for reading and writing.
|
abstract void |
onCreate(SQLiteDatabase db)
Called when the database is created for the first time.
|
void |
onOpen(SQLiteDatabase db)
Called when the database has been opened.
|
abstract void |
onUpgrade(SQLiteDatabase db,
int oldVersion,
int newVersion)
Called when the database needs to be upgraded.
|
public SQLiteOpenHelper(Context context, java.lang.String name, SQLiteDatabase.CursorFactory factory, int version)
getWritableDatabase(java.lang.String)
or getReadableDatabase(java.lang.String)
is called.context
- to use to open or create the databasename
- of the database file, or null for an in-memory databasefactory
- to use for creating cursor objects, or null for the defaultversion
- number of the database (starting at 1); if the database is older,
onUpgrade(net.sqlcipher.database.SQLiteDatabase, int, int)
will be used to upgrade the databasepublic SQLiteOpenHelper(Context context, java.lang.String name, SQLiteDatabase.CursorFactory factory, int version, SQLiteDatabaseHook hook)
getWritableDatabase(java.lang.String)
or getReadableDatabase(java.lang.String)
is called.context
- to use to open or create the databasename
- of the database file, or null for an in-memory databasefactory
- to use for creating cursor objects, or null for the defaultversion
- number of the database (starting at 1); if the database is older,
onUpgrade(net.sqlcipher.database.SQLiteDatabase, int, int)
will be used to upgrade the databasehook
- to run on pre/post key eventspublic SQLiteDatabase getWritableDatabase(java.lang.String password)
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.
close()
is calledSQLiteException
- if the database cannot be opened for writingpublic SQLiteDatabase getWritableDatabase(char[] password)
public SQLiteDatabase getReadableDatabase(java.lang.String password)
getWritableDatabase(java.lang.String)
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(java.lang.String)
may succeed, in which case the read-only
database object will be closed and the read/write object will be returned
in the future.getWritableDatabase(java.lang.String)
or close()
is called.SQLiteException
- if the database cannot be openedpublic SQLiteDatabase getReadableDatabase(char[] password)
public void close()
public abstract void onCreate(SQLiteDatabase db)
db
- The database.public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
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.
db
- The database.oldVersion
- The old database version.newVersion
- The new database version.public void onOpen(SQLiteDatabase db)
SQLiteDatabase.isReadOnly()
before
updating the database.db
- The database.