public abstract class BaseOpenHelper
extends android.database.sqlite.SQLiteOpenHelper
// A subclass of BaseOpenHelper. MyOpenHelper helper = ...; SQLiteDatabase db = null; try { // Open the database in read mode. openReadable() // acquires a read lock and then opens the database // by getReadableDatabase(). db = helper.openReadable(); ...... } finally { // Close the database and release the read lock. helper.closeReadable(db); }
| Constructor and Description |
|---|
BaseOpenHelper(android.content.Context context,
String name,
android.database.sqlite.SQLiteDatabase.CursorFactory factory,
int version)
Constructor.
|
BaseOpenHelper(int version)
Constructor.
|
BaseOpenHelper(android.database.sqlite.SQLiteDatabase.CursorFactory factory,
int version)
Constructor.
|
BaseOpenHelper(String name,
android.database.sqlite.SQLiteDatabase.CursorFactory factory,
int version)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
closeReadable(android.database.sqlite.SQLiteDatabase db)
Close a database which has been opened by
openReadable(). |
void |
closeWritable(android.database.sqlite.SQLiteDatabase db)
Close a database which has been opened by
openWritable(). |
ReentrantReadWriteLock |
getLock()
Get the internal read-write lock instance.
|
android.database.sqlite.SQLiteDatabase |
openReadable()
Open the database in read mode.
|
android.database.sqlite.SQLiteDatabase |
openWritable()
Open the database in write mode.
|
public BaseOpenHelper(android.content.Context context,
String name,
android.database.sqlite.SQLiteDatabase.CursorFactory factory,
int version)
super(context, name, factory, version);public BaseOpenHelper(String name, android.database.sqlite.SQLiteDatabase.CursorFactory factory, int version)
this(App.getInstance().getContext(), name, factory, version);
public BaseOpenHelper(android.database.sqlite.SQLiteDatabase.CursorFactory factory,
int version)
this(App.getInstance().getContext().getPackageName(), factory, version);
public BaseOpenHelper(int version)
this(null, version);public android.database.sqlite.SQLiteDatabase openReadable()
This method acquires a read lock first, then calls
SQLiteOpenHelper.getReadableDatabase(). If getReadableDatabase()
throws an SQLiteException, the read lock is released and
the exception is re-thrown. The SQLiteDatabase instance
returned by this method has to be closed by
closeReadable(SQLiteDatabase), not by SQLiteDatabase.close(). Otherwise, the read lock is not released.
public void closeReadable(android.database.sqlite.SQLiteDatabase db)
openReadable().
This method closes the passed database by calling
db.close() and then releases
a read lock (even if db.close() failed).
db - A database which has been opened by openReadable().
If null is given, nothing is done (any read lock
is released).public android.database.sqlite.SQLiteDatabase openWritable()
This method acquires a write lock first, then calls
SQLiteOpenHelper.getWritableDatabase(). If getWritableDatabase()
throws an SQLiteException, the write lock is released and
the exception is re-thrown. The SQLiteDatabase instance
returned by this method has to be closed by
closeWritable(SQLiteDatabase), not by SQLiteDatabase.close(). Otherwise, the write lock is not released.
public void closeWritable(android.database.sqlite.SQLiteDatabase db)
openWritable().
This method closes the passed database by calling
db.close() and then releases
a write lock (even if db.close() failed).
db - A database which has been opened by openWritable().
If null is given, nothing is done (any write lock
is released).public ReentrantReadWriteLock getLock()
Copyright © 2016. All rights reserved.