public abstract class DffSQLiteOpenHelper
extends java.lang.Object
Constructor and Description |
---|
DffSQLiteOpenHelper(java.lang.String name,
java.lang.String path,
android.database.sqlite.SQLiteDatabase.CursorFactory factory,
int version)
Create a helper object to create, open, and/or manage a database.
|
DffSQLiteOpenHelper(java.lang.String name,
java.lang.String path,
android.database.sqlite.SQLiteDatabase.CursorFactory factory,
int version,
android.database.DatabaseErrorHandler errorHandler)
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.
|
java.lang.String |
getDatabaseName()
Return the name of the SQLite database being opened, as given to
the constructor.
|
java.lang.String |
getDbPath()
Returns the path of the db file.
|
android.database.sqlite.SQLiteDatabase |
getReadableDatabase()
Create and/or open a database.
|
android.database.sqlite.SQLiteDatabase |
getWritableDatabase()
Create and/or open a database that will be used for reading and writing.
|
void |
onConfigure(android.database.sqlite.SQLiteDatabase db)
Called when the database connection is being configured, to enable features
such as write-ahead logging or foreign key support.
|
abstract void |
onCreate(android.database.sqlite.SQLiteDatabase db)
Called when the database is created for the first time.
|
void |
onDowngrade(android.database.sqlite.SQLiteDatabase db,
int oldVersion,
int newVersion)
Called when the database needs to be downgraded.
|
void |
onOpen(android.database.sqlite.SQLiteDatabase db)
Called when the database has been opened.
|
abstract void |
onUpgrade(android.database.sqlite.SQLiteDatabase db,
int oldVersion,
int newVersion)
Called when the database needs to be upgraded.
|
void |
setWriteAheadLoggingEnabled(boolean enabled)
Enables or disables the use of write-ahead logging for the database.
|
public DffSQLiteOpenHelper(java.lang.String name, java.lang.String path, android.database.sqlite.SQLiteDatabase.CursorFactory factory, int version)
getWritableDatabase()
or
getReadableDatabase()
is called.name
- 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(android.database.sqlite.SQLiteDatabase, int, int)
will be used to upgrade the database; if the database is
newer, onDowngrade(android.database.sqlite.SQLiteDatabase, int, int)
will be used to downgrade the databasepublic DffSQLiteOpenHelper(java.lang.String name, java.lang.String path, android.database.sqlite.SQLiteDatabase.CursorFactory factory, int version, android.database.DatabaseErrorHandler errorHandler)
getWritableDatabase()
or getReadableDatabase()
is called.
Accepts input param: a concrete instance of DatabaseErrorHandler
to be
used to handle corruption when sqlite reports database corruption.
name
- of the database file, or null for an in-memory databasepath
- 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(android.database.sqlite.SQLiteDatabase, int, int)
will be used to upgrade the database; if the database is
newer, onDowngrade(android.database.sqlite.SQLiteDatabase, int, int)
will be used to downgrade the databaseerrorHandler
- the DatabaseErrorHandler
to be used when sqlite reports database
corruption, or null to use the default error handler.public java.lang.String getDbPath()
public java.lang.String getDatabaseName()
public void setWriteAheadLoggingEnabled(boolean enabled)
enabled
- True if write-ahead logging should be enabled, false if it
should be disabled.SQLiteDatabase.enableWriteAheadLogging()
public android.database.sqlite.SQLiteDatabase getWritableDatabase()
onCreate(android.database.sqlite.SQLiteDatabase)
, onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
and/or onOpen(android.database.sqlite.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()
.
close()
is calledandroid.database.sqlite.SQLiteException
- if the database cannot be opened for writingpublic android.database.sqlite.SQLiteDatabase getReadableDatabase()
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.
Like getWritableDatabase()
, this method may
take a long time to return, so you should not call it from the
application main thread, including from
ContentProvider.onCreate()
.
getWritableDatabase()
or close()
is called.android.database.sqlite.SQLiteException
- if the database cannot be openedpublic void close()
public void onConfigure(android.database.sqlite.SQLiteDatabase db)
This method is called before onCreate(android.database.sqlite.SQLiteDatabase)
, onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
,
onDowngrade(android.database.sqlite.SQLiteDatabase, int, int)
, or onOpen(android.database.sqlite.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 SQLiteDatabase.enableWriteAheadLogging()
SQLiteDatabase.setForeignKeyConstraintsEnabled(boolean)
,
SQLiteDatabase.setLocale(java.util.Locale)
, SQLiteDatabase.setMaximumSize(long)
,
or executing PRAGMA statements.
db
- The database.public abstract void onCreate(android.database.sqlite.SQLiteDatabase db)
db
- The database.public abstract void onUpgrade(android.database.sqlite.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.
This method executes within a transaction. If an exception is thrown, all changes will automatically be rolled back.
db
- The database.oldVersion
- The old database version.newVersion
- The new database version.public void onDowngrade(android.database.sqlite.SQLiteDatabase db, int oldVersion, int newVersion)
onUpgrade(android.database.sqlite.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.
db
- The database.oldVersion
- The old database version.newVersion
- The new database version.public void onOpen(android.database.sqlite.SQLiteDatabase db)
SQLiteDatabase.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(android.database.sqlite.SQLiteDatabase)
instead.
db
- The database.