使用orm创建数据库

时间:2015-10-05 12:50:22

标签: android sqlite orm

我在我的应用程序中第一次使用ORMlite数据库。我已经参考了一个教程,但是我没有完全相同的事情,我无法解决错误。我的代码如下: -

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;
    }
}

我使用modifiersDao = getDao(ModifierDetails.class)的行;给出错误

错误:(76,30)错误:D的推断类型无效;推断类型不符合声明的边界 推断:道 bound(s):Dao 其中D,T是类型变量: D扩展方法getDao(Class)中声明的Dao T扩展了方法getDao(Class)

中声明的Object

请帮助:(

1 个答案:

答案 0 :(得分:1)

您的声明在上面是错误的:

私人Dao&lt; ItemDetails,Integer&gt; modifiersDao;

但getMofifierDao()返回Dao&lt; ModifierDetails,Integer&gt;