Android ORMLite 没有这样的表异常

标签 android ormlite

我在我的 Android 应用程序中使用 ORMlite(版本 4.48)

这是我的 ORMLiteHelper:

public class TrainingOrmLiteHelper extends OrmLiteSqliteOpenHelper {
public static final String TAG = TrainingOrmLiteHelper.class.getSimpleName();

public static final String DATABASE_NAME = "marek.sqlite";
public static final int DATABASE_VERSION = 7;

private Dao<DataModel, Integer> modelDao = null;

public TrainingOrmLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
    try {
        TableUtils.clearTable(connectionSource, DataModel.class);
        Log.d(TAG, "Created");
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
    try {
        TableUtils.dropTable(connectionSource, DataModel.class, true);
        Log.d(TAG, "Updated");
    } catch (SQLException e) {
        e.printStackTrace();
    }
    onCreate(database, connectionSource);
}

public Dao<DataModel, Integer> getModelDao() throws SQLException {
        if (modelDao == null) {
            modelDao = getDao(DataModel.class);
        }
        return modelDao;
    }
}

和我的数据模型对象类

    @DatabaseTable(tableName = "data_model")
    public class DataModel {

    @DatabaseField(generatedId = true)
    int id;

    @DatabaseField
    String title;

    @DatabaseField
    String description;

    public DataModel() {
    }

    public DataModel(String title, String description) {
        this.title = title;
        this.description = description;
    }

}

当我尝试用我的数据做一些事情时:

TrainingOrmLiteHelper helper = new TrainingOrmLiteHelper(this);
        DataModel item = new DataModel();
        item.description = "ELO";
        item.title = "blah blah";
        helper.getModelDao().create(item);

我收到错误:

E/SQLiteLog﹕ (1) no such table: data_model

最佳答案

查看您的代码,您实际上还没有创建 表,您需要添加 createTableIfNotExistOrmLiteSqliteOpenHelper 类的 onCreate 方法。

TableUtils.createTableIfNotExists(ConnectionSource connectionSource, Class<T> dataClass);

关于Android ORMLite 没有这样的表异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30916126/

相关文章:

android - android中谷歌地图上的圆形动画

java - ORMLite 异常 : java. sql.SQLException: ResultSet 已请求

android - 使用 GSON 将嵌套对象展平为目标对象

android - 我如何在 ORMLite 中将 "map"作为所有数据库实体扩展的基础对象

android - ORMLite 重置所有表

java - 我可以在商业项目中使用 OrmLite 吗?

android - 是否可以使用 android 应用内购买包月

android - ViewPager 中光标驱动的内容

android - 如何检查是否存在 GPS 传感器?

android - 在 Firebase Auth 中,如何检测匿名用户已升级到例如Facebook的用户?