在Android中使用Greendao同时连接两个具有不同架构的数据库

6

这个项目中有两个数据库,其中一个是在打开应用时创建的,另一个是通过assets提供的。

当生成DaoSession时,DaoSession会为所有模型创建。

同时,gradle文件中的Schema用于两个数据库。

如何区分这两个数据库及其架构?

2个回答

5

你需要创建两个不同的类,它们都继承自org.greenrobot.greendao.database.DatabaseOpenHelper。这两个不同的类DevOpenHelperForDatabase1DevOpenHelperForDatabase2将处理与数据库相关的输入输出。从下面的代码中很容易理解如何创建具有相同或不同模式、表或实体的两个不同的数据库:

public class App extends Application {
private DaoSessionForDatabase1 mDaoSessionForDatabase1;
private DaoSessionForDatabase2 mDaoSessionForDatabase2;

@Override
public void onCreate() {
    super.onCreate();

    //Create Doa session for database1
    DevOpenHelperForDatabase1 devOpenHelperForDatabase1 = new DevOpenHelperForDatabase1(this,
        "database1-db");
    Database databse1 =  devOpenHelperForDatabase1.getWritableDb();
    mDaoSessionForDatabase1 = new DaoMasterForDatabase1(databse1).newSession();

    //Create Doa session for database2
    DevOpenHelperForDatabase2 devOpenHelperForDatabase2 = new DevOpenHelperForDatabase2(this,
        "database2-db");
    Database databse2 =  devOpenHelperForDatabase2.getWritableDb();
    mDaoSessionForDatabase2 = new DaoMasterForDatabase2(databse2).newSession();
}

public DaoSessionForDatabase1 getDaoSessioForDatabase1() {
    return mDaoSessionForDatabase1;
}

public DaoSessionForDatabase2 getDaoSessioForDatabase2() {
    return mDaoSessionForDatabase2;
}
}

例如,您可以从活动中访问以下相同或不同的模式、表或实体:

// get the Schema1 DAO for Database1
DaoSessionForDatabase1 daoSessionForDatabase1 = ((App)    getApplication()).getDaoSessioForDatabase1();
Schema1Dao schema1Dao = daoSessionForDatabase1.getSchema1Dao();

// get the Schema2 DAO for Database2
DaoSessionForDatabase2 daoSessionForDatabase2 = ((App)  getApplication()).getDaoSessioForDatabase2();
Schema2Dao schema2Dao = daoSessionForDatabase2.getSchema2Dao();

更新2:以上内容可以被舍弃,但方法是相同的。根据下面评论的讨论进行了更新:
我在示例greenDAO -> examples中进行了更改。
package org.greenrobot.greendao.example;

import android.app.Application;

import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.example.DaoMaster.DevOpenHelper;

public class App extends Application {
/** A flag to show how easily you can switch from standard SQLite to  the encrypted SQLCipher. */
public static final boolean ENCRYPTED = true;

private DaoSession daoSession;

private DaoSession daoSession1;

@Override
public void onCreate() {
    super.onCreate();

    DevOpenHelper helper = new DevOpenHelper(this, ENCRYPTED ? "notes-db-encrypted" : "notes-db");
    Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
    daoSession = new DaoMaster(db).newSession();


    DevOpenHelper helper1 = new DevOpenHelper(this, "notes1-db");
    Database db1 = helper1.getWritableDb();
    daoSession1 = new DaoMaster(db1).newSession();
}

public DaoSession getDaoSession() {
    return daoSession;
}

public DaoSession getDaoSession1() {
    return daoSession1;
}
}

现在,请在NoteActivity.java中做出以下更改。
//Add below class members
private static boolean switchDbBetweenOneAndTwo = false;
private NoteDao noteDao2;
private Query<Note> notesQuery2;

//In on craete add the following as the last statement after   notesQuery = noteDao.queryBuilder().orderAsc(NoteDao.Properties.Text).build();
@Override
public void onCreate(Bundle savedInstanceState) {
......
Log.d("Database 1", "notesQuery.list()="+notesQuery.list().toString());

    // get the note DAO for Database2
    DaoSession daoSessionForDb2 = ((App) getApplication()).getDaoSession1();
    noteDao2 = daoSessionForDb2.getNoteDao();

    // query all notes, sorted a-z by their text
    notesQuery2 = noteDao2.queryBuilder().orderAsc(NoteDao.Properties.Text).build();
    Log.d("Database 2", "notesQuery2.list()="+notesQuery2.list().toString());
    updateNotes();
}

//Replace updateNotes as
private void updateNotes() {
    List<Note> notes = notesQuery.list();
    List<Note> notes2 = notesQuery2.list();
    notes.addAll(notes2);
    notesAdapter.setNotes(notes);
}

//Replace addNote as
private void addNote() {
    String noteText = editText.getText().toString();
    editText.setText("");

    final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
    String comment = "Added on " + df.format(new Date());

    Note note = new Note();
    note.setText(noteText);
    note.setComment(comment);
    note.setDate(new Date());
    note.setType(NoteType.TEXT);
    if(!switchDbBetweenOneAndTwo){
        note.setText(noteText + " In database 1");
        noteDao.insert(note);
    }
    else {
        note.setText(noteText + " In database 2");
        noteDao2.insert(note);
    }
    Log.d("DaoExample", "Inserted new note, ID: " + note.getId());
    switchDbBetweenOneAndTwo = true;
    updateNotes();
}

我没有更改gradle文件,也没有添加任何内容,因为这对我来说没有任何意义。


我曾经在几年前使用不同的DAO为黑莓做了同样的事情。我期待着这可能是基于经验的方式。 - Anurag Singh
不幸的是,在GreenDao中并非如此,模式设置在gradle文件中。我认为这将会有所不同。 - MBH
@MBH 现在检查一下吗? - Anurag Singh
很遗憾,版本3不支持多个模式。请参考此链接:https://github.com/greenrobot/greenDAO/issues/356。无论如何,我会测试您发布的代码。 - MBH
1
感谢您,如果我有遗漏的地方,请告诉我。谢谢您。 - Anurag Singh
显示剩余7条评论

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接