Creating more than 1 *.db file with greenDao android ORM library

喜夏-厌秋 提交于 2019-12-06 07:34:46

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
}

You can generate several schemas for the project and also for several projects.
Take a look at a simple framework I wrote for that purpose:

GitHub : https://github.com/aivarsda/GreenDAOGenerator
- All entities have Serializable implementation.
- Ability to generate several schemas to the project.
- Ability to generate for several projects. (Handling different projects from one place)

Basically, you can add your schemas to the list:

private List <Schema> getSchemas()
{
List <Schema> schemaList = new ArrayList<Schema>();
        schemaList.add(new StoreSchema(1, "com.aivarsda.greendao_fw.orm.store",DEFAULT_PROJ_OUTPUT_PATH));
        schemaList.add(new TreeSchema(1, "com.aivarsda.greendao_fw.orm.tree",DEFAULT_PROJ_OUTPUT_PATH));

        // You may generate for several projects.
        // Pass another project output path via constructor.
        schemaList.add(new StoreSchema(1,"com.aivarsda.anotherproj.orm.store","../../_anotherproj/src"));

        return schemaList;
    }

And then generate the output for thees schemas:

public void generate()
{
    List <Schema> schemaList = getSchemas();
    try 
    {
        for (int i=0; i<schemaList.size(); i++)
        {
            Schema schema = schemaList.get(i);
            new DaoGenerator().generateAll(schema, ((AGenSchema)schema).getOutRelativePath());
        }
    }catch (Exception e) {
        e.printStackTrace();
    }
}

just give a varible to DaoMaster.DevOpenHelper,like this:

public class DaoUtil {
public static DaoSession daoSession;
private static DaoMaster daoMaster;
private static SQLiteDatabase db;

public static void generateDatabase(Context context, String userId) {
    DaoMaster.DevOpenHelper openHelper = new DaoMaster
            .DevOpenHelper(context, "user_" + userId, null);
    db = openHelper.getWritableDatabase();
    daoMaster = new DaoMaster(db);
    daoSession = daoMaster.newSession();
}

}

when you give a different value to this DaoUtil, it will generate a different db instance file your app,and you can keep more than 1 instance of daosession if you want operate more than 1 database in same time.

but there is problem,all of your database will generate all tables you had defined in bean class,that means all database instance have same tables. Hope this answers is helpful to you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!