Android ORMLite no such table exception

2.1k views Asked by At

I am using ORMlite (version 4.48) in my Android app

This is my 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;
    }
}

and my data model object class

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

}

and when I try to do something with my data like:

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

Im getting the error:

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

There are 1 answers

1
Ed Holloway-George On BEST ANSWER

Looking at your code, you haven't actually created the table, you'll need to add createTableIfNotExist to the onCreate method of your OrmLiteSqliteOpenHelper class.

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