2017-02-19 13 views
6

Tôi có 2 cơ sở dữ liệu trong dự án, một trong số chúng được tạo khi tôi mở ứng dụng, phần còn lại được cung cấp tài sản.Sử dụng nhiều hơn 1 Cơ sở dữ liệu ở Greendao với 2 lược đồ khác nhau - Android

Khi DaoSession được tạo, DaoSession được tạo cho tất cả các kiểu máy.

cũng lược đồ trong tệp gradle được sử dụng cho cả hai cơ sở dữ liệu

Làm cách nào để phân biệt giữa 2 cơ sở dữ liệu và lược đồ của chúng?

Trả lời

6

Bạn cần phải tạo hai lớp khác nhau mở rộng org.greenrobot.greendao.database.DatabaseOpenHelper. Hai lớp khác nhau DevOpenHelperForDatabase1DevOpenHelperForDatabase2 sẽ xử lý db realted I/o. Đó là versy dễ hiểu từ mã dưới đây để tạo ra hai cơ sở dữ liệu khác nhau với cùng một và khác nhau schema hoặc bàn hoặc tổ chức:

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; 
} 
} 

Bạn có thể truy cập vào giống và khác nhau schema hoặc bàn hoặc tổ chức như dưới đây từ Hoạt động như một ví dụ :

// 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(); 

Cập nhật 2 :: Ở trên có thể bị hủy nhưng cách tiếp cận sẽ giống nhau. Cập nhật xong dựa trên các cuộc thảo luận trong các ý kiến ​​dưới đây:

tôi đã thực hiện những thay đổi trong ví dụ 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; 
} 
} 

Bây giờ làm cho chnages sau trong 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(); 
} 

tôi đã không thay đổi trong gradle tập tin hoặc thêm bất cứ điều gì vì nó không làm cho bất kỳ ý nghĩa với tôi.

+0

DaoSessionForDatabase tệp được tạo tự động. Chúng tôi không thể đặt tên hoặc thay đổi tên của chính mình. Bạn đã kiểm tra mã này chưa? nó có thực sự hoạt động không? hoặc bạn đang mong đợi cách nó có thể được? – MBH

+0

Tôi đã làm tương tự cho blackberry một số năm trở lại bằng cách sử dụng dao khác nhau. Tôi hy vọng nó có thể dựa trên kinh nghiệm. –

+0

thật không may, đây không phải là trường hợp trong GreenDao, lược đồ được đặt trong tệp gradle. Tôi nghĩ nó sẽ khác. – MBH

Các vấn đề liên quan