2

我了解ORMLite Android 示例。但是,我想用 ORMlite 映射不止一个类。

在创建与“SimpleData”类匹配的表的示例中,我们使用了这个:

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

如何创建所有表(与我的类匹配)?

4

2 回答 2

3

所以也许我不明白你在做什么。在上面的SimpleData示例中,您引用了DatabaseHelper. 在该onCreate方法中,您可以创建任意数量的类:

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) {
        ...

对于一个工作程序。如果您看一下ClickCounter 示例,它会在其onCreate方法中创建两个实体:

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);
    }
}
于 2012-04-17T17:25:01.973 回答
2

与创建第一个方法相同:多次调用 TableUtils.createTable 方法,您将向其传递表示数据库中表的类

于 2012-04-17T17:21:46.583 回答