androidormlite

Android ORMlite - more than one table / classes


I have a problem by using the ORMlite framework with more than one table / classes.

I´ve got one DatabaseHelper.class which extends the OrmLiteSqliteOpenHelper which looks like this (important parts):

public class DatabaseHelper<T> extends OrmLiteSqliteOpenHelper {

    private static final String DATABASE_NAME = "database.db";
    private static final int DATABASE_VERSION = 6;

    private Dao<T, Integer> myDao = null;
    private Class<T> type;

    public DatabaseHelper(Context context, Class<T> type) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.type = type;
    }
    
    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, type);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Dao<T, Integer> getmyDataDao() throws SQLException {
        if (myDao == null) {
            myDao = getDao(type);
        }
        return myDao;
    }
}

I use 2 classes (Admin1, Admin2) in which I want to save 2 different objects (Object1, Object2) in the database. Here the code I used for this.

Amin1:

static {
    OpenHelperManager.setOpenHelperFactory(new SqliteOpenHelperFactory() {
        public OrmLiteSqliteOpenHelper getHelper(Context context) {
            return new DatabaseHelper<Object1>(context, Object1.class);
        }
    });
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.object1);
    Dao<Object1, Integer> myDaoObject1 = getHelper().getmyDataDao();
    Object1 o1 = new Object1("Name of Object 1");
    int ret = myDaoObject1.create(o1);
}

Admin2:

static {
    OpenHelperManager.setOpenHelperFactory(new SqliteOpenHelperFactory() {
        public OrmLiteSqliteOpenHelper getHelper(Context context) {
            return new DatabaseHelper<Object2>(context, Object2.class);
        }
    });
}


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.object2);
    Dao<Object2, Integer> myDaoObject2 = getHelper().getmyDataDao();
    Object2 o2 = new Object2("Name of Object 2");
    int ret = myDaoObject2.create(o2);
}

It works fine for the first object I save (in Admin1 or Admin2). But if I save object1 in Admin1 and after that I want to save object2 in Admin2 or reverse (the order is not decisive), I get following error in the line "int ret = myDaoObject2.create(o2);":

Unable to run stmt on object com.example.Object2@44c408f0: INSERT INTO `object2` (`name` ) VALUES (?)

There is no error in creating the Object2 o2 in the code. Not before the line "int ret = myDaoObject2.create(o2);" the error occurs ... I have no idea why there is an "?" in the value. One line before the object looks fine.

Has anybody an idea?

Thanks a lot!

Update

Unfortunately I couldn`t solve the problem.

I´ve tried now to close the OrmLiteSqliteOpenHelper at the onPause() and/or at the onStop() of the activitiy:

myHelper.close();

Since then I get the error:

"Unable to destroy activity - null pointer exception"

I can't understand this error.

Does anybody have an idea?


Solution

  • Isn't your DB file completely rewritten on admin2 initialization? Do you really need two different Helper instances? If you just need to have two different dao objects for them - use one shared helper and just add a new getAdmin2Dao method.

    HTH

    EDIT (Adding some code):

     private Dao<Profile, String> profileDao = null;
     private Dao<Network, String> networkDao = null;
    
     @Override
     public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
                try {
                        Log.i(DBHelper.class.getName(), "onCreate");
    
                        TableUtils.createTable(connectionSource, Network.class);
                        TableUtils.createTable(connectionSource, Profile.class);
    
                } catch (SQLException e) {
                        Log.e(DBHelper.class.getName(), "Can't create database", e);
                        throw new RuntimeException(e);
                }
     }
    
     public Dao<Profile, String> getProfileDao() throws SQLException {
        if (profileDao == null) {
           profileDao = getDao(Profile.class);
        }
        return profileDao;
     }
    
     // ... Same for the other DAO
    
     @Override
     public void close() {
        super.close();
    
        networkDao = null;
        profileDao = null;
     }