Frage

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());
}
War es hilfreich?

Lösung

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
}

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top