xutils3 database upgrade (markdown version)

Keywords: Android Database Java

xutils is a good android development framework, which reduces a lot of code in the process of using. But there are also a few things to pay attention to. For other uses, please see Detailed usage of xutils3

1. Notes

Unlike the comments of scalpel, the comments of xutils are in runtime (ps: I don't understand either). It is found that when clicking two buttons frequently, only one button will be responded to. If you set onclicklister by findview byid, there will be no such bug.

So when we use the click function, we can use the notes properly. At present, we only find that frequent clicks will cause problems, not necessarily buried in other places.

2. Database upgrade

When our business is improving day by day, the previously established database fields may need to be modified. We configure the database code as follows

    DbManager.DaoConfig daoConfig =newDbManager.DaoConfig()

    .setDbName("myapp.db")//Set database name

    xutils.db.setDbDir(newFile("/mnt/sdcard/"))//Set the database path, which is stored in the app's private directory by default

    .setDbVersion(2)//Set the version number of the database

    .setDbOpenListener(newDbManager.DbOpenListener() {//Set listening for database opening

        @Override
        public void onDbOpened(DbManager db) {//Enable database to support multi-threaded operation, improve performance and greatly accelerate write
        db.getDatabase().enableWriteAheadLogging();
}
})
    .setDbUpgradeListener(newDbManager.DbUpgradeListener() {//Set listening for database updates

        @Override
        public void onUpgrade(DbManager db,intoldVersion,intnewVersion) {

}

})  .setTableCreateListener(newDbManager.TableCreateListener() {//Set listening for table creation
        @Override
        public void onTableCreated(DbManager db, TableEntity table){
        Log.i("JAVA","onTableCreated: "+ table.getName());
}
});

We can fill in any number in setDbVersion (x), of course, according to our own actual situation

When we need to change the field, we can fill in the number of x+n, and then

.setDbUpgradeListener(newDbManager.DbUpgradeListener() {//Set listening for database updates

@Override 
public void onUpgrade(DbManager db,intoldVersion,intnewVersion) {

  //No previous data required

  db.delete(x.class);

  //Previous data required

  db.addColumn(x.class,"test");//New fields

  db.saveOrUpdate(db.findall());//If there is this isId in the current table, the data will be updated. If there is no isId, the data will be added

}

})

Thanks to android, thanks to open source

Posted by jchemie on Sat, 07 Dec 2019 17:16:48 -0800