私は2つのデータベースをプロジェクトに持っています。そのうちの1つはアプリケーションを開くときに作成され、もう1つはアセットが提供されます。Greendaoの2つの異なるスキーマで1つ以上のデータベースを使用する - Android
DaoSessionが生成されると、すべてのモデルに対してDaoSessionが作成されます。また
のGradleファイル内のスキーマ
は、両方のデータベースのために使用されているどのように私は2つのデータベースとそのスキーマを区別することができますか?
私は2つのデータベースをプロジェクトに持っています。そのうちの1つはアプリケーションを開くときに作成され、もう1つはアセットが提供されます。Greendaoの2つの異なるスキーマで1つ以上のデータベースを使用する - Android
DaoSessionが生成されると、すべてのモデルに対してDaoSessionが作成されます。また
のGradleファイル内のスキーマ
は、両方のデータベースのために使用されているどのように私は2つのデータベースとそのスキーマを区別することができますか?
Greendaoバージョン3では、2つ以上のスキーマを持つことはできません。
github.com/greenrobot/greenDAO/issues/356
彼らはまた、彼らのウェブサイト上で書いたように:
注意を複数のスキーマがcurrently not supported when using the Gradle pluginあること。当分の間、発電機 プロジェクトを引き続き使用してください。
彼らはまだこの機能を新しいGreenDaoに追加しました。
org.greenrobot.greendao.database.DatabaseOpenHelper
を拡張する2つの異なるクラスを作成する必要があります。 DevOpenHelperForDatabase1
とDevOpenHelperForDatabase2
の2つの異なるクラスは、db realted I/Oを処理します。あなたは一例として、Activityから怒鳴ると同じと異なるスキーマやテーブルやエンティティにアクセスすることができます
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;
}
}
:同じと異なるスキーマやテーブルや実体を持つ2つの異なるデータベースを作成するには、以下のコードから理解することがversy簡単です。 :
// 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();
}
で次chnagesを作る作らそれは私には分かりません。
DaoSessionForDatabase自動生成ファイル。名前を付けたり、名前を変更することはできません。このコードをテストしましたか?それは本当に効果的ですか?あなたはそれがあるかもしれないと期待していますか? – MBH
私はブラックベリーのために何年か前に異なったDAOを使って同じことをしました。私はそれが経験に基づいているかもしれないと期待しています。 –
残念ながら、これはGreenDaoのケースではなく、スキーマはgradleファイルに設定されています。私はそれが違うと思う。 – MBH