greendao 加载本地db文件

greendao 打开assets 下边db

1.首先复制db 文件到   data/data/包名/databases 下 

    /**
     * 复制db
     */
    private void copyDataBase(String dbname) throws IOException {
        // Open your local db as the input stream
        InputStream myInput = this.getAssets().open(dbname);
        // Path to the just created empty db
        File outFileName = this.getDatabasePath(dbname);

        if (!outFileName.exists()) {
            outFileName.getParentFile().mkdirs();

            // Open the empty db as the output stream
            OutputStream myOutput = new FileOutputStream(outFileName);
            // transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }
    }

然后生成dao

package com.jiuku.bible.db.entry;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;

@Entity(
        nameInDb = "chapters",
        createInDb = false
)
 /**
     *  createInDb = false  不需要创建表
     */
public class Chapters {

    /**
     * 
     */
    @Id
    @Property(nameInDb = "id")
    private Long id;

  
    @Property(nameInDb = "parent_id")
    private String parent_id;


    private String content;

    private int count;
  
    private String title;

  
    private int chapter;

  
    @Property(nameInDb = "book_id")
    private int book_id;

@Generated(hash = 1462527718)
public Chapters(Long id, String parent_id, String content, int count,
        String title, int chapter, int book_id) {
    this.id = id;
    this.parent_id = parent_id;
    this.content = content;
    this.count = count;
    this.title = title;
    this.chapter = chapter;
    this.book_id = book_id;
}

@Generated(hash = 120664092)
public Chapters() {
}

public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}

public String getParent_id() {
    return this.parent_id;
}

public void setParent_id(String parent_id) {
    this.parent_id = parent_id;
}

public String getContent() {
    return this.content;
}

public void setContent(String content) {
    this.content = content;
}

public int getCount() {
    return this.count;
}

public void setCount(int count) {
    this.count = count;
}

public String getTitle() {
    return this.title;
}

public void setTitle(String title) {
    this.title = title;
}

public int getChapter() {
    return this.chapter;
}

public void setChapter(int chapter) {
    this.chapter = chapter;
}

public int getBook_id() {
    return this.book_id;
}

public void setBook_id(int book_id) {
    this.book_id = book_id;
}

   
}

3.

package com.jiuku.bible.db;

import com.jiuku.bible.MyApplication;
import com.jiuku.bible.db.dao.DaoMaster;
import com.jiuku.bible.db.dao.DaoSession;

/**
 *  @author swt
 *  @date 2021/3/2
 *  @describe 管理圣经计划
 */
public class GreenDaoManager {

    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    private static GreenDaoManager mInstance;
    //单例
    public static GreenDaoManager getInstance(){
        if (mInstance==null){
            //保证异步处理安全操作
            synchronized (GreenDaoManager .class){
                if (mInstance==null){
                    mInstance=new GreenDaoManager ();
                }
            }
        }
        return mInstance;
    }

    private GreenDaoManager (){
        if (mInstance==null){
            DaoMaster.DevOpenHelper openHelper=new DaoMaster.DevOpenHelper(MyApplication.getAppContext(),"xxx.db",null);
            mDaoMaster=new DaoMaster(openHelper.getWritableDatabase());
            mDaoSession=mDaoMaster.newSession();

        }
    }

    public DaoMaster getMaster(){
        return mDaoMaster;
    }
    public DaoSession getSession(){
        return mDaoSession;
    }
    public DaoSession getNewSession(){
        mDaoSession=mDaoMaster.newSession();
        return mDaoSession;
    }
}

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页