GreenDao operation using an existing database .db

GreenDao operation using an existing database .db

Editor's Preface

If your project uses some of GreenDao then actually building a database, build operating table are hidden from you, and only you can do whatever you like according to XXXDao of the CRUD. Benpian not take space to explain how to configure GreenDAO, now GreenDAO configure it simply too comfortable than the previous version, and will not crawl under article to learn their own, no less than ten minutes you can be configured. Then little understood at the ORM (object-relational mapping) frame is a what can start reading the article.

I encountered a problem in the project, that is city targeting feature, originally a simple city targeting Android developers but this is not a problem, but it requires the positioning results with the same micro-channel applet and the background to be held data format, which means that the city's name friends, friends in the provinces of the region, must be consistent. Said very confused, a JS file our city on the map, on the left is a copy from the Internet, and then into a JSON format. We are right in the format on the left side and then control the city selector applet (applet own components) one by one manually changed out (do not ask why, we might free it). Once the results are changed as follows, in no small program selector Taiwan, Hong Kong and Macao. Then Beijing in Beijing called, and then a lot of points below the city district, the rest is such a similar change. After the change we have in this rightmost (that is, to compromise applet) as the front-end back end mobile applet end unified regional format, and then generate the JSON and JS files, JS file will go back to call the front, JSON file format to the mobile terminal to use.
Here Insert Picture Description

But in the city's selection process, I lazy to use an open source on GitHub city selector CityPicker , very grateful to the author, I save a lot of time. But this can not select a custom data source of the city, I can only download the source code view of how to achieve, and open the project directory to see the authors use the assets folder china_cities_v2.db city database.
Open the database file, you can see the city's data source is like this, my God, the name is not the same, there is no "city" word (Behind his) back Beijing:
Here Insert Picture Description
So this is the origin of the problem, our aim is to define good city data source, which is JSON formatted data, replace the china_cities_v2.db database (born programmer could love toss it, Wulian).

Ready condition

  • New construction, will be the JSON format files area_190114.jsonand database files china_cities_v2.dbinto a folder to assets.
  • Configuration GreenDAO.

Code

The part of a total of two steps:

1, the copy of the database file to the application database folder

Because it can not directly manipulate the database assets folder, so we can only be copied to applications in database-specific databases folder.
Code below, without considering other circumstances existing database, copy only:

    private void copyDataBase() throws IOException {
        InputStream myInput = this.getAssets().open(DB_NAME);
        File outFileName = this.getDatabasePath(DB_NAME);

        outFileName.getParentFile().mkdirs();
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

After completion of this step can be opened in the AS Device File Explorer , then data -> data -> your application package name -> databases folder you can see it copied in the database, as shown below.
Here Insert Picture Description

2, the database operation using GreenDAO

Use GreenDAO operation, then the database need to create an entity class of a city, because we need to modify the database tables in the city. The following fields correspond to:

@Entity(
        nameInDb = "cities",
        createInDb = false
)
public class City {

    @Id
    @Property(nameInDb = "id")
    public Long id;

    @Property(nameInDb = "c_name")
    private String name;

    @Property(nameInDb = "c_province")
    private String province;

    @Property(nameInDb = "c_pinyin")
    private String pinyin;

    @Property(nameInDb = "c_code")
    private String code;

}

There are several points to note:

  • @Entity (nameInDb = "cities", createInDb = false), nameInDb is the table name in the database, see table above the picture database. createInDb here because we are using an existing database table, so do not need to create a new table.

  • @Property (nameInDb = "c_code"), there is a comment property, but also to keep the table field names correspond. Then type attributes do not mistake such as a Long type id, rather than an int id.

This class is created after clicking Build -> Make Project, the project will be built automatically, and insert the appropriate code. After the build is complete we started using GreenDAO operate:

DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(
                this, DB_NAME, null);
DaoMaster daoMaster = new DaoMaster(openHelper.getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
CityDao cityDao = daoSession.getCityDao();
//先删除原先数据库中的所有数据
cityDao.deleteAll();
//这里是从JSON中获取到所有城市,返回的一个城市列表,然后循环执行插入操作
List<City> cities = AreaUtils.getAllCities(this);
for (City city : cities) {
    cityDao.insert(city);
}

The code above operation is complete database files modified after the export view, is not it, "Beijing" into "Beijing", the other also get:
Here Insert Picture Description

So far, the toss is completed, as it is reasonable not toss product manager saying things (Behind his).

The paper also use the Pinyin string open source library Promega / TinyPinyin , thank the author.

annex

After the modification attached JSON file and modified the database file .

Published 40 original articles · won praise 47 · views 70000 +

Guess you like

Origin blog.csdn.net/u010976213/article/details/86496254