Saving data is a requirement of any application. In Android, we can save data mainly in three ways.
- SQLite
- SharedPreferences
- File System
The easy way to save data is by using the SQLite database and working through SqliteOpenHelper
.
We need to write raw queries and manipulation through cursors
.
It becomes difficult to manage when the code base becomes large and manual errors are more possible.
The solution to this is using Data Access Objects
or DAOs.
It is an open source Android ORM (Object-Relational Mapping)
making development for SQLite databases easy
Here is the app build using greenDAOstep by step for you to understand.
- Add Gradle dependency in app/build.gradle.
- Add Gradle plugin for the Annotation processing in the project’s build.gradle.
- GreenDAO requires us to create the schema of the table in the form of a class
- Explore the DAO generated classes
- Construct a class to communicate with the database
- Test the table in the Main activity
- specify the schema version of the database
- Add Gradle dependency in app/build.gradle.
greenDao = '3.1.1'
implementation "org.greenrobot:greendao:$rootProject.greenDao"
- Add Gradle plugin for the Annotation processing in the project’s build.gradle.
classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.1’
Then use this plugin in the app/build.gradle, below com.android.application plugin
apply plugin: ‘org.greenrobot.greendao’
- GreenDAO requires us to create the schema of the table in the form of a class
@Entity(nameInDb = "user_entity")
public class UserEntity {
@Id(autoincrement = true)
@Property(nameInDb = "user_id")
private Long userId;
@Property(nameInDb = "user_name")
private String userName;
@Property(nameInDb = "user_state")
private String userState;
}
When you build the project, it will generate getter, setters, and constructors for this class.
- @Entity
- It turns the Java class User into a database-backed entity. This will also instruct greenDAO to generate the necessary code.
- @Id
- It selects a long/ Long property as the entity ID. In database terms, it’s the primary key. The parameter autoincrement is a flag to make the ID value ever increasing (not reusing old values).
- @Property
- It lets you define a non-default column name, which the property is mapped to. If absent, greenDAO will use the field name in a SQL-ish fashion.
- Explore the DAO generated classes
public class BaseRepo {
private static BaseRepo instance = null;
protected DaoSession daoSession;
public BaseRepo() {
daoSession = DAOSessionHolder.getInstance().getDaoSession();
if (daoSession == null) {
MySQLiteOpenHelper helper = new MySQLiteOpenHelper(Application.getContext(), "greenDaoDemo.db");
Database db = helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
DAOSessionHolder.getInstance().putDaoSession(daoSession);
}
}
public static BaseRepo getInstance() {
if (instance == null) {
instance = new BaseRepo();
}
return instance;
}
}
- DaoMaster: This class defines the methods for database creation and manipulation.
- DaoSession: This class provides the DAO classes for accessing the database tables.
- MySQLiteOpenHelper class
public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {
public MySQLiteOpenHelper(Context context, String name) {
super(context, name);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
MigrationHelper.migrate(db, DaoHelper.getAllDaos());
}
}
- Construct a class to communicate with the database
public class UserOperations extends BaseRepo {
private static UserOperations instance = null;
private UserEntityDao dao;
private UserOperations(Context context) {
super();
dao = daoSession.getUserEntityDao();
}
public static UserOperations getInstance(Context context) {
if (instance == null) {
instance = new UserOperations(context);
}
return instance;
}
/**
* @return list of user entity from the table name UserEntity in the database
*/
public List<UserEntity> getUserEntity() {
return dao.queryBuilder()
.list();
}
}
- Test the table in the Main activity
public class MainActivity {
private Context context;
private UserOperations userOperations;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context=this;
userOperations = UserOperations.getInstance(context);
//Here we get the list of record from the table name UserEntity in the database
List<UserEntity> userEntityList=userOperations.getUserEntityList();
}
}
- specify the schema version of the database
You will need to specify the schema version of the database so that you can get the old version and new version when the app is upgraded. To specify this add greendao schema in the app/build.gradle.
greendao {
android {
...
}
schemaVersion 1
daoPackage "com.example.greendaodemo.database.base"
}
Copyright 2018 Devendra Mehra
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.