android堆栈模式,android - 在Greendao中使用2个以上的数据库,使用2个不同的模式 - Android - 堆栈内存溢出...

您需要创建两个不同的类,这些类扩展了org.greenrobot.greendao.database.DatabaseOpenHelper 。 这两个不同的类DevOpenHelperForDatabase1和DevOpenHelperForDatabase2将处理db实现的I / DevOpenHelperForDatabase2 从下面的代码中可以很容易地理解,创建具有相同和不同模式或表或实体的两个不同的数据库:

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;

}

}

您可以访问与Activity相同的不同架构或表或实体作为示例:

// 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 ::上面的内容可以丢弃,但方法也是一样的。 根据以下评论中的讨论完成更新:

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中NoteActivity.java以下NoteActivity.java

//Add below class members

private static boolean switchDbBetweenOneAndTwo = false;

private NoteDao noteDao2;

private Query 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 notes = notesQuery.list();

List 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文件进行任何更改或添加任何内容,因为它对我没有任何意义。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值