When writing a project, we encounter the operation of database. The creation and use of database provided by Android feels a little complicated. We wonder if there is a better wheel-framework or tool class to use. We find litepal, an object-oriented way to develop database, is similar to hibernate. In a world where everything is an object, it feels so cool to use.
LitePal is an open source android database framework (https://github.com/LitePal Framework/LitePal). It uses the object-relational mapping (ORM) model to transform the operation of the database into the operation of the object.
Use steps of Litepal:
1. Add in Android studio in the gradle file:
[java] view plain copy
dependencies { compile 'org.litepal.android:core:1.3.1' }
You can also download the jar package and add it to your project. The download address is the same as the github address above.
2. Create a new litepal.xml file under the assets directory. The file is a configuration file. litpal gets the relevant information from the reflection of the file.
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?> <litepal> <pre name="code" class="html"> <!--Database name-->
[html] view plain copy
<pre-name="code" class="html"> <!--database version number-->
I created a table in my project.
3. Configure LitepalApplication, because the operation of the database requires context, configure LitepalApplication, do not use context.
The configuration is as follows:
Looking at the source code of LitePal Application, inheriting the application can use a tConext to get the global context. The source code is as follows:
[java] view plain copy
/** * Get the global application context. * * @return Application context. * @throws org.litepal.exceptions.GlobalException */ public static Context getContext() { if (sContext == null) { throw new GlobalException(GlobalException.APPLICATION_CONTEXT_IS_NULL); } return sContext; }
If your application has a layer of inheritance, such as
public class MyOwnApplication extends BaseApplication { ... }
At this time, you can use the following methods to register:
public class MyOwnApplication extends BaseApplication { @Override public void onCreate() { super.onCreate(); LitePalApplication.initialize(this);
} ... }
4. Build a table and create a new class to inherit DataSupport, which is in litepal. The following is my project:
[java] view plain copy
package com.wjustudio.phoneManager.javaBean; import org.litepal.crud.DataSupport; /** * songwenju on 16-4-20 : 12 : 23. * E-mail: songwenju@outlook.com */ public class BlackNumInfo extends DataSupport{ private String blackNum; private int mode; public String getBlackNum() { return blackNum; } public void setBlackNum(String blackNum) { this.blackNum = blackNum; } public int getMode() { return mode; } public void setMode(int mode) { this.mode = mode; } public BlackNumInfo() { } @Override public String toString() { return "BlackNumInfo{" + "blackNum='" + blackNum + '\'' + ", mode=" + mode + '}'; } }
Upgrading the database, add some fields to the class and modify the version number of xml that was configured before.
5. curd operations on database tables
1) Save the data and call the object's save method
2) Modify the data according to the situation: if you know id, use find first, then save. The example given by github is as follows:
[java] view plain copy
Album albumToUpdate = DataSupport.find(Album.class, 1); albumToUpdate.setPrice(20.99f); // raise the price albumToUpdate.save();
If you want to modify all the data:
Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price albumToUpdate.update(id);
Or there are certain conditions of data, although updateAll is also used here, to update all the data that meet the conditions, if the condition is the primary key or the only column related, it is equivalent to modifying a data.
Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price albumToUpdate.updateAll("name = ?", "album");
3) delete
Know Id:
DataSupport.delete(Song.class, id);
Delete eligible data:
DataSupport.deleteAll(Song.class, "duration > ?" , "350");
4) Query data
Know Id:
Song song = DataSupport.find(Song.class, id);
All queries:
List allSongs = DataSupport.findAll(Song.class);
The query satisfies the criteria:
List songs = DataSupport.where("name like ?", "song%").order("duration").find(Song.class);
My project uses MVP design pattern. The following code is attached:
[java] view plain copy
package com.wjustudio.phoneManager.biz; import com.wjustudio.phoneManager.javaBean.BlackNumInfo; import java.util.List; /** * songwenju on 16-4-20 : 12 : 27. * E-mail: songwenju@outlook.com */ public interface IBlackNumBiz { /** * Get all the blacklists * @return */ List<BlackNumInfo> getAllBlackNum(); /** * Add a blacklist * @param blackNumInfo */ void insertBlackNum(BlackNumInfo blackNumInfo); /** * Get a blackNumInfo by phone number * @param blackNum */ BlackNumInfo getBlackNumInfo(String blackNum); /** * Delete blacklist information * @param blackNumInfo */ void deleteBlackNum(BlackNumInfo blackNumInfo); /** * Upgrade blacklist information * @param blackNumInfo */ void updateBlackNum(BlackNumInfo blackNumInfo); /** * Whether Blacklist Information Exists * @param blackNum * @return */ boolean isExist(String blackNum); }
[java] view plain copy
package com.wjustudio.phoneManager.biz; import android.content.ContentValues; import com.wjustudio.phoneManager.javaBean.BlackNumInfo; import org.litepal.crud.DataSupport; import java.util.List; /** * songwenju on 16-4-20 : 13 : 23. * E-mail: songwenju@outlook.com */ public class BlackNumBizImpl implements IBlackNumBiz { public static final int BLACK_NUM_PHONE = 1; public static final int BLACK_NUM_SMS = 2; public static final int BLACK_NUM_ALL = 3; @Override public List<BlackNumInfo> getAllBlackNum() { return DataSupport.findAll(BlackNumInfo.class); } @Override public void insertBlackNum(BlackNumInfo blackNumInfo) { blackNumInfo.saveThrows(); } @Override public BlackNumInfo getBlackNumInfo(String blackNum) { List<BlackNumInfo> blackNumInfos = DataSupport.where("blackNum = ?", blackNum).find(BlackNumInfo.class); return blackNumInfos.get(0); } @Override public void deleteBlackNum(BlackNumInfo blackNumInfo) { if (!blackNumInfo.isSaved()) { blackNumInfo.save(); } blackNumInfo.delete(); } @Override public void updateBlackNum(BlackNumInfo blackNumInfo) { ContentValues values = new ContentValues(); values.put("mode", blackNumInfo.getMode()); DataSupport.updateAll(BlackNumInfo.class, values, "blackNum = ?", blackNumInfo.getBlackNum()); } @Override public boolean isExist(String blackNum) { List<BlackNumInfo> blackNumInfos = DataSupport.where("blackNum = ?", blackNum).find(BlackNumInfo.class); return blackNumInfos !=null && blackNumInfos.size()> 0; } }
In this way, BlackNumBizImpl objects can be used to add, delete and modify the database in the project.