de.theappguys.winzigsql
Class WinzigDbFileHelper
java.lang.Object
android.database.sqlite.SQLiteOpenHelper
de.theappguys.winzigsql.WinzigDbFileHelper
public class WinzigDbFileHelper
- extends android.database.sqlite.SQLiteOpenHelper
Extension of SQLiteOpenHelper that handles initialization of
a database by using a database from a file in the assets.
Automatically initializes the database from a db file in the assets.
The file must be named like your database, so if your db is named
"foo", it looks for the file "assets/foo.db"
This helper has only very simple upgrade conventions, it simply completely
replaces the current db with the one from the assets. If you need to preserve
data that might have been added to the db, make sure you overwrite onCreate
and handle that case manually.
Works hand-in-hand with the WinzigDbProvider
|
Constructor Summary |
WinzigDbFileHelper(android.content.Context context,
String dbName,
int dbVersion)
Let your custom db provider extends this class and call the superconstructor like so: |
|
Method Summary |
protected void |
copyDb()
copies the database from the assets to the db file location |
protected boolean |
dbExists()
|
boolean |
dropDb()
|
android.database.sqlite.SQLiteDatabase |
getReadableDatabase()
|
android.database.sqlite.SQLiteDatabase |
getWritableDatabase()
|
void |
onCreate(android.database.sqlite.SQLiteDatabase db)
(non-Javadoc) |
void |
onUpgrade(android.database.sqlite.SQLiteDatabase db,
int oldVersion,
int newVersion)
Just overwrites the existing database with the newest one from the assets. |
| Methods inherited from class android.database.sqlite.SQLiteOpenHelper |
close, onOpen |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
LOG_TAG
public static final String LOG_TAG
- See Also:
- Constant Field Values
WinzigDbFileHelper
public WinzigDbFileHelper(android.content.Context context,
String dbName,
int dbVersion)
- Let your custom db provider extends this class and call the superconstructor like so:
private MyDbHelper(final Context context) {
super (context, "myDbName", 42);
}
Then, to obtain instances, implement a get Instance method like this:
private static MyDbHelper instance = null;
public static MyDbHelper(final Context context) {
if (instance == null) {
return (instance = new MyDbHelper(context))
} else {
return instance;
}
}
getReadableDatabase
public android.database.sqlite.SQLiteDatabase getReadableDatabase()
- Overrides:
getReadableDatabase in class android.database.sqlite.SQLiteOpenHelper
getWritableDatabase
public android.database.sqlite.SQLiteDatabase getWritableDatabase()
- Overrides:
getWritableDatabase in class android.database.sqlite.SQLiteOpenHelper
onCreate
public void onCreate(android.database.sqlite.SQLiteDatabase db)
- (non-Javadoc)
- Specified by:
onCreate in class android.database.sqlite.SQLiteOpenHelper
- See Also:
SQLiteOpenHelper.onCreate(android.database.sqlite.SQLiteDatabase)
onUpgrade
public void onUpgrade(android.database.sqlite.SQLiteDatabase db,
int oldVersion,
int newVersion)
- Just overwrites the existing database with the newest one from the assets.
If you store custom data in your database after copying it from the assets,
you need to overwrite this method and make sure the old data is preserved.
- Specified by:
onUpgrade in class android.database.sqlite.SQLiteOpenHelper
- See Also:
SQLiteOpenHelper.onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
copyDb
protected void copyDb()
- copies the database from the assets to the db file location
- Throws:
IOException
dbExists
protected boolean dbExists()
- Returns:
- if the database was already copied or not
dropDb
public boolean dropDb()
- Returns:
- true: db was deleted.
Copyright © 2013. All Rights Reserved.