Before I knew it, I realized that I hadn't written anything for almost a month. I have been busy with work and life recently. I have experienced a lot of things this month, some are happy and some are sad. Fortunately, everything has come. When I am free, I want to write something. I want to share my usage process with greenDAO, an open source framework about databases that I like to use at work.
greenDAO is a light and fast ORM for Android that maps objects to SQLite databases. Thanks to highly optimized Android, greenDAO provides excellent performance and consumes minimal memory.
Configuration
buildscript {
repositories {
jcenter()
mavenCentral()
}dependencies {
classpath ':gradle:2.3.1'
classpath ':greendao-gradle-plugin:3.2.2'
}
}apply plugin: ''
apply plugin: ''android {
buildToolsVersion '25.0.2'
compileSdkVersion 25defaultConfig {
applicationId ""
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "3"testInstrumentationRunner ""
}
}greendao {
schemaVersion 1000
}dependencies {
compile ':greendao:3.2.2'// Here we must add this dependency when we use an encrypted database
compile ':android-database-sqlcipher:3.5.6'compile ':appcompat-v7:25.3.1'
compile ':recyclerview-v7:25.3.1'
}= false
Database initialization
After the configuration is successful, we need to create an entity class to generate the corresponding table. @Entity means that this entity class will generate the corresponding table in the database. @Id means that the field is id (setting the Long type means that the id will automatically grow):
@Entity public class Province { @Id private Long id; private String provinceName; private String provinceCode; }
After writing these, compile the project: Build—Make Project. After the compilation is successful, the system will help us generate the corresponding constructor and get/set methods, and will also generate DaoMaster and DaoSession under our package. Now we can initialize the database:
public class App extends Application { // Here we define a flag to switch the standard mode and encryption mode of the database public static final boolean ENCRYPTED = true; private DaoSession daoSession; @Override public void onCreate() { (); DevOpenHelper helper = new DevOpenHelper(this, ENCRYPTED ? "province-db-encrypted" : "province-db"); Database db = ENCRYPTED ? ("super-secret") : (); daoSession = new DaoMaster(db).newSession(); } public DaoSession getDaoSession() { return daoSession; } }
If we want to operate the entity class, we need to operate it through DAO, for example, we want to operate itProvince
Entity class, then we must first get oneProvinceDao
,passProvinceDao
We can add, delete, modify and check the database:
private ProvinceDao provinceDao; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_mine_fix_school); DaoSession daoSession = ((App) getApplication()).getDaoSession(); provinceDao = (); }
Add data
List<Province> provinceList = ().build().list(); if (() > 0) { // Process it when there is data in the database }else { //When the database has no data, obtain network data for data storage (areaCode) .subscribeOn(()) .observeOn(()) .subscribe(new MySubscriber<List<Province>>() { @Override public void onNext(List<Province> provinceList) { for (Province province : provinceList) { //Insert data (province); } }); }
Delete data
In fact, deleting data is the same as modifying data. We need to find the data first, and then operate on the data:
for (Province province : provinceList) { //Delete data (province); }
Modify data
Here I have only added a query condition, that is, id is equal to 10, and the last unique means that only one data can be queryed:
Province province = ().where((10)).build().unique(); if (province == null) { //The user does not exist }else { ("Modify Zhejiang Province"); //Modify the data (province); }
Query data
In fact, the above deletion and modification have already involved querying. There are many very useful functions in the query. Here I only used one between data indicating that the query id is between 10 and 20.
List<Province> provinces = ().where((10, 20)).build().list(); for (Province province : provinces) { //Output query content ("TAG","search:" + ()); }
Replenish
- eq: equal to
- notEq: not equal to
- gt: greater than
- lt: less than ge: greater than or equal to
- le: less than or equal to
- Between: Range between a certain value
Finish
Well, here some simple functions of greenDAO can be implemented. Many databases are used during the work process, whether they write natively or some open source frameworks. Until now, I feel that greenDAO should be considered a very useful open source framework, and I like it very much. I want to further explore its mystery.
This is the article about the configuration and use of greenDAO on Android database. For more related Android greenDAO content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!