Neha Tyagi
Neha Tyagi

Reputation: 3821

Creating database using orm

I am using ORMlite database for the first time in my application. i have taken reference from a tutorial, but instead of doing all the things exactly same i am not able to resolve an error. My code is below:-

DatabaseHelper:

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private static final String DATABASE_NAME = "qzeodemo.db";
    private static final int DATABASE_VERSION = 1;


    private Dao<ModifierDetails, Integer> itemsDao;
    private Dao<ItemDetails, Integer> modifiersDao;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
    }

    @Override
    public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
        try {

            // Create tables. This onCreate() method will be invoked only once of the application life time i.e. the first time when the application starts.
            TableUtils.createTable(connectionSource,ItemDetails.class);
            TableUtils.createTable(connectionSource,ModifierDetails.class);

        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Unable to create datbases", e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource, int oldVer, int newVer) {
        try {

            // In case of change in database of next version of application, please increase the value of DATABASE_VERSION variable, then this method will be invoked
            //automatically. Developer needs to handle the upgrade logic here, i.e. create a new table or a new column to an existing table, take the backups of the
            // existing database etc.

            TableUtils.dropTable(connectionSource, ItemDetails.class, true);
            TableUtils.dropTable(connectionSource, ModifierDetails.class, true);
            onCreate(sqliteDatabase, connectionSource);

        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Unable to upgrade database from version " + oldVer + " to new "
                    + newVer, e);
        }
    }

    // Create the getDao methods of all database tables to access those from android code.
    // Insert, delete, read, update everything will be happened through DAOs

    public Dao<ItemDetails,Integer> getItemDao() throws SQLException {
        if (itemsDao == null) {

       itemsDao = getDao(ItemDetails.class);
        }
        return itemsDao;
    }

    public Dao<ModifierDetails, Integer> getMofifierDao() throws SQLException {
        if (modifiersDao == null) {
            modifiersDao = getDao(ModifierDetails.class);
        }
        return modifiersDao;
    }
}

The line where i am using modifiersDao = getDao(ModifierDetails.class); is giving error

Error:(76, 30) error: invalid inferred types for D; inferred type does not conform to declared bound(s) inferred: Dao bound(s): Dao where D,T are type-variables: D extends Dao declared in method getDao(Class) T extends Object declared in method getDao(Class)

Please help :(

Upvotes: 0

Views: 108

Answers (1)

AndroidEnthusiast
AndroidEnthusiast

Reputation: 6657

Your declaration is wrong above:

private Dao< ItemDetails, Integer > modifiersDao;

but getMofifierDao() returns Dao< ModifierDetails, Integer>

Upvotes: 1

Related Questions