【问题标题】:ORMlite creating more than one table ANDROIDORMlite 创建多个表 ANDROID
【发布时间】:2012-04-17 17:11:54
【问题描述】:

我了解ORMLite Android examples。但是,我想用 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);
    }

如何创建我的所有表格(与我的班级相匹配)?

【问题讨论】:

    标签: android ormlite


    【解决方案1】:

    所以也许我不明白你想要做什么。在上面的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 example,它会在其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);
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多