本文介绍了创造了超过1 * .db的与Android的greenDao ORM库文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用greenDao库()来管理我的Android应用程序的数据库。一切工作正常,但我不能找到一种方法来创建很多* db文件。我所有的表都在同一个单一文件i.ex. books.db。
有没有办法告诉greenDao把书籍相关的books.db表和相关作家表中authors.db?

预先感谢帮助。

编辑:结果
好,我解决了这个问题。你可以把他们在不同的模式让许多* db文件,然后使用generatedAll每个模式i.ex:

  {尝试    新DaoGenerator()generateAll(schema1,_path)。
    新DaoGenerator()generateAll(SCHEMA2,_path);    的System.out.println(成功的所有文件生成:+ _path);
    }赶上(IOException异常五){
        e.printStackTrace();
    的System.out.println(IOException异常错误:+ e.getMessage());
    }赶上(例外五){
    e.printStackTrace();
    的System.out.println(异常错误:+ e.getMessage());
}


解决方案

下面是充满code为解决我的问题。例如,我想2的数据库文件:数据库1和DATABASE2然后我会做这样的事情。请注意,把他们在不同的模式将使GreenDao创建2 db文件。

 公共类ExampleDaoGenerator {
    //你的第一个数据库架构版本
    私有静态最终诠释_data1SchemaVer = 1;
    //你的第二个数据库架构版本
    私有静态最终诠释_data2SchemaVer = 1;    //你的第一个数据库包
    私有静态最后弦乐_data1Package =com.noitra.data.database1;
    //你的第二个数据库包
    私有静态最后弦乐_data2Package =com.noitra.data.database2;    //路径保存生成的文件
    私有静态最后弦乐_path =../MyApplication/src-gen;    公共静态无效的主要(字串[] args){        //数据库1模式
        架构data1Schema =新的架构(_data1SchemaVer,_data1Package);
        data1Schema.enableKeepSectionsByDefault();        //模式的Database2
        架构data2Schema =新的架构(_data2SchemaVer,_data2Package);
        data2Schema.enableKeepSectionsByDefault();        //添加方法,可以定义你的数据库
        addData1Method(data1Schema);
        addData2Method(data2Schema);        //生成数据库
        尝试{
            DaoGenerator根=新DaoGenerator();            //生成数据库1
            gen.generateAll(data1Schema,_path);
            //生成DATABASE2
            gen.generateAll(data2Schema,_path);            的System.out.println(成功的所有文件生成:+ _path);
        }赶上(IOException异常五){
            e.printStackTrace();
            的System.out.println(IOException异常错误:+ e.getMessage());
        }赶上(例外五){
            e.printStackTrace();
            的System.out.println(异常错误:+ e.getMessage());
        }
    }私有静态无效addData1Method(架构模式){
//定义DATABASE1 HERE
}私有静态无效addData2Method(架构模式){
//定义DATABASE2 HERE
}

I'm using greenDao library (http://greendao-orm.com/) for managing my android app database. Everything works fine, but I can't find a way to create many *.db files. All my tables are in the same single file i.ex. books.db.Is there a way to tell greenDao to put books related tables in books.db and authors related tables in authors.db?

Thanks in advance for help.

EDIT:
Ok I've solved the problem. You can make many *.db files by putting them in different schemas and then use generatedAll for each schemas i.ex:

try {

    new DaoGenerator().generateAll(schema1, _path);
    new DaoGenerator().generateAll(Schema2, _path);

    System.out.println("Successfully generated all files to: " + _path);
    } catch (IOException e) {
        e.printStackTrace();
    System.out.println("IOException error: " + e.getMessage());
    } catch (Exception e) {
    e.printStackTrace();
    System.out.println("Exception error: " + e.getMessage());
}
解决方案

Here is full code for solution to my problem. For example I want 2 database files: database1 and database2 then I would do something like that. Note that putting them in different schemas will make GreenDao to create 2 .db files.

public class ExampleDaoGenerator {
    // Your first database schema version
    private static final int _data1SchemaVer = 1;
    // Your second database schema version
    private static final int _data2SchemaVer = 1;

    // Your first database package
    private static final String _data1Package = "com.noitra.data.database1";
    // Your second database package
    private static final String _data2Package = "com.noitra.data.database2";

    // Path to save generated files
    private static final String _path = "../MyApplication/src-gen";

    public static void main(String[] args) {

        // Database1 schema
        Schema data1Schema = new Schema(_data1SchemaVer, _data1Package);
        data1Schema.enableKeepSectionsByDefault();

        // Database2 schema
        Schema data2Schema = new Schema(_data2SchemaVer, _data2Package);
        data2Schema.enableKeepSectionsByDefault();

        //Add methods where you define your databases
        addData1Method(data1Schema);
        addData2Method(data2Schema);

        // Generate your databases
        try {
            DaoGenerator gen = new DaoGenerator();

            // Generate database1
            gen.generateAll(data1Schema, _path);
            // Generate database2
            gen.generateAll(data2Schema, _path);

            System.out.println("Successfully generated all files to: " + _path);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IOException error: " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception error: " + e.getMessage());
        }
    }

private static void addData1Method(Schema schema) {
// DEFINE YOUR DATABASE1 HERE
}

private static void addData2Method(Schema schema) {
// DEFINE YOUR DATABASE2 HERE
}

这篇关于创造了超过1 * .db的与Android的greenDao ORM库文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 09:26
查看更多