ORMLite on Android not calling onCreate - android

Using ORMLite v 4.40, I try to get my app running, but it seems to ignore the onCreate function
My DatabaseHelper looks like this (snippet style)
public class ORMLiteHelper extends OrmLiteSqliteOpenHelper {
private Context databaseContext;
private static String DATABASE_NAME = "InVinoVeritas";
private static int DATABASE_VERSION = 1;
public ORMLiteHelper(Context context) {
super (context, DATABASE_NAME, null, DATABASE_VERSION);
Log.v("ORMLiteHelper", "Cosntructor");
...
#Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
Log.v("DatabaseHelper", "onCreate");
...
#Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
Log.v("DatabaseHelper", "onUpgrade");
...
My MainActivity calls the DatabaseHelper as described:
public class MainActivity extends OrmLiteBaseActivity<ORMLiteHelper> {
I have tried re-installing the application, upgrading the database version, nothing works.
I do see the constructor call (including typo :-), the onCreate and onUpgrade however are not called.
Any help appreciated
Barry

Create instance of ORMLiteHelper and call getWritableDatabase(). When database is not created then onCreate will be invoked.

Related

Static object has separate instance in BroadCastReceiver execution?

I have a static, singleton instance of a SQLiteOpenHelper implementation.
public class MyDbHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 5;
public static final String DATABASE_NAME = "myapp.db";
public static SQL_CREATE_MY_TABLE ="some correct table creation sql";
public static SQL_DELETE_MY_TABLE="some correct table delete sql";
...
public MyDbHelper (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_MY_TABLE);
db.execSQL(SQL_CREATE_OTHER_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_MY_TABLE);
db.execSQL(SQL_DELETE_OTHER_TABLE);
onCreate(db);
}
}
I have another wrapper over the helper as:
public class MyAppDb {
private static MyDbHelper mydbHelper;
public static MyDbHelper getDbHelper(){
if(null == mydbHelper){
mydbHelper= new MyDbHelper(ctx);
return mydbHelper;
}else{
return mydbHelper;
}
}
}
I am using the mydbHelper from this getDbHelper() in my main Ui code(at various places), as well as in a BroadcastReceiver of an Alarmmanager that executes every 5 mins, and in another system BroadcastReceiver.
Sample usage:
SQLiteDatabase db = MyAppDb.getDbHelper().getReadableDatabase();
Intermittently, I get SQLiteDatabaseLockedException and SQLiteException on the the db implementations from the above mydbHelper.
It seems as though although static, there are two different instances of the MyDbHelper occuring at the same time.
What is happening? Can a static object have a separate instance created from BroadCastReceiver?
Or I am doing it wrong?

Sqlite app not working on my device, but does on my emulator

In my app I have used Sqlite. When I test the app in the emulator it runs ok but when I test the app on my device, it fails because the Helper class not pass by the OnCreate method. My java code is below.
public static void startDB(Context context) {
DBHelper = new DataBaseHelper(context);
DBHelper.getWritableDatabase();
}
private static class DataBaseHelper extends SQLiteOpenHelper {
public DataBaseHelper(Context context) {
super(context, NOMBRE_BASE_DATOS, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(USER_CREATE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
for (String table : ALL_TABLES) {
db.execSQL("DROP TABLE IF EXISTS " + table);
}
onCreate(db);
}
}
The framework only calls onCreate() if the database does not exist.
To force onCreate(), remove the old database file: clear your app's data in app manager, or just uninstall and reinstall.

Create SQLite DB in Android library

I've seen a lot of ties between the onCreate and onOpen methods and SQLite database management in Android.
I am an iOS developer and I'm trying to "translate" (so to speak) my cocoa library so it could be used on Android. I need to create an SQLite database at runtime. I don't have an activity - since this is a library I'm creating. It seems I can't create a DB without an activity, is this correct ?
To create sqlite db, you don't basically need an activity in the library.it needs a context!!,
You can have a method in library/class which basically take a context in input/parameter and create database.
This context can be passed from application activity or service or receiver.
I don't see why you should not be able to open a database without an activity. You need to extend SQLiteOpenHelper.
public class MyDatabase {
private final DatabaseHelper databaseHelper;
private SQLiteDatabase db;
private static final String DATABASE_NAME = "com.my.db";
private static final int DATABASE_VERSION = 1;
public MyDatabase(Context context) {
databaseHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
//use db.execSQL to create the database
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// use db.execSQL to update (modify) the database
}
}
public SQLiteDatabase open() throws SQLException {
db = databaseHelper.getWritableDatabase();
return db;
}
}

Good schema to delete database file in SQLiteOpenHelper.onDowngrade()

I have an existing database based on SQLiteOpenHelper that has several versions and code to upgrade it and that works fine. But in case a user installs an older version of the app (that expects a lower database version) it will currently crash - the ContentProvider using it can't access the database. I'd like to prevent it from crashing but I don't want to actually downgrade the database - adding the code to do that would be pain. Dropping all tables would certainly work but starting with a fresh file is imo cleaner and less error prone.
That's about what the database helper looks like - nothing special
public class MyDbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_NAME = "my.db";
public MyDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
onUpgrade(db, 0, DATABASE_VERSION);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion < 1) db.execSQL("CREATE TABLE A...");
if (newVersion < 2) db.execSQL("CREATE TABLE B...");
if (newVersion < 3) db.execSQL("CREATE TABLE C...");
}
#Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// I'd like to delete the database here
// the only problem is that I can't from here
// since this is called in the middle of getWritableDatabase()
// and SQLiteDatabase has no .recreate() method.
}
}
The possible ways I've come up to do that are:
Do it from the outside: catch exceptions in the ContentProvider, delete the file and request to open the database again. - I don't like that since it's not the responsibility of the provider.
Replacing SQLiteOpenHelper with my own copy of that class that deletes the file instead of calling onDowngrade - Problem is that it's using package private parts of SQLiteDatabase (e.g. .lock()) which I can't replace without duplicating SQLiteDatabase too (that would probably result in duplicating the whole sqlite stack).
Is there any good approach to do that or do I have to go the DROP TABLES way e.g. like described here?
I've figured out a way that works nicely by extending SQLiteOpenHelper and all I need to do in MyDbHelper is to extend this class.
public abstract class DeletingSQLiteOpenHelper extends SQLiteOpenHelper {
private static final String TAG = DeletingSQLiteOpenHelper.class.getSimpleName();
private final File mDatabaseFile;
public DeletingSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version,
DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
mDatabaseFile = context.getDatabasePath(name);
}
public DeletingSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
mDatabaseFile = context.getDatabasePath(name);
}
#Override
public synchronized SQLiteDatabase getWritableDatabase() {
try {
return super.getWritableDatabase();
} catch (SQLiteDowngradeFailedException e) {
// that's our notification
}
// try to delete the file
mDatabaseFile.delete()
// now return a freshly created database
return super.getWritableDatabase();
}
#Override
public final void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// throwing a custom Exception to catch it in getWritableDatabase
throw new SQLiteDowngradeFailedException();
}
// that's the exception
static class SQLiteDowngradeFailedException extends SQLiteException {
public SQLiteDowngradeFailedException() {}
public SQLiteDowngradeFailedException(String error) {
super(error);
}
}
}

SQLiteOpenHelper not calling onCreate

This is the fist time I've used SQLiteOpenHelper (or databases on android). When I get a writeable database I was wondering why onCreate isnt being called on each new instance of the class. Am I doing something wrong?
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "MyDatabase.db";
private static final int DATABASE_VERSION = 1;
private String PrSQLcmd = "";
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE IF NOT EXISTS Contact(Firstname TEXT, LastName TEXT");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
In SQLiteOpenHelper, the meaning of 'onCreate' is different from what it is in an Activity. Here,'onCreate' is called only once, which is the first time you create the database. The next time you run the app, the database is already there, so it won't call 'onCreate'. Your object level initialization should be done in the constructor and not in 'onCreate'
To see 'onCreate' being called, either manually delete the db file, or simply uninstall the app.

Categories

Resources