Youness
Youness

Reputation: 65

ORMlite creating more than one table ANDROID

I understand the ORMLite Android examples. However, I have more than one class that I want to map with the ORMlite.

In the example to create the table which match with "SimpleData" class we used this :

public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
    try {
        Log.i(DatabaseHelper.class.getName(), "onCreate");
        TableUtils.createTable(connectionSource, SimpleData.class);
    } 

    catch (SQLException e) {
        Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
        throw new RuntimeException(e);
    }

How can I create all my tables (which matches my classes) ?

Upvotes: 2

Views: 3274

Answers (2)

Gray
Gray

Reputation: 116878

So maybe I'm not understanding what you are trying to do. In the SimpleData example above, you are quoting code from the DatabaseHelper. In the onCreate method you can create any number of classes that you want:

public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
    try {
        Log.i(DatabaseHelper.class.getName(), "onCreate");
        TableUtils.createTable(connectionSource, SimpleData.class);
        TableUtils.createTable(connectionSource, AnotherData.class);
        TableUtils.createTable(connectionSource, AndAnotherData.class);
        ...
    } catch (SQLException e) {
        ...

For a working program. if you take a look at the ClickCounter example, it creates two entities in its onCreate method:

public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
    try {
        TableUtils.createTable(connectionSource, ClickGroup.class);
        TableUtils.createTable(connectionSource, ClickCount.class);
    } catch (SQLException e) {
        Log.e(DatabaseHelper.class.getName(), "Unable to create datbases", e);
    }
}

Upvotes: 3

Tomislav Novoselec
Tomislav Novoselec

Reputation: 4620

the same way you created the first one: with multiple calls of TableUtils.createTable method, to whom you'll pass the class representing the table in database

Upvotes: 2

Related Questions