Some time ago, there was a simple collection function in the project. In view of the simple demand, WE prepared to directly use the third party. My colleague said that this was very easy to use, so we used Sugar in the project. Here’s a summary of the basic use of Sugar.

1. Inject the Sugar dependencies into your project’s build.gradle

compile 'com. Making. Satyan: sugar: 1.5'
Copy the code

2. Configure related parameters in the. Androidmanifest.xml file of the project

<! <meta-data android:name= database <meta-data android:name= database <meta-data android:name= database <meta-data android:name= database <meta-data android:name= database"DATABASE"
            android:value="The directory in which your database operations are located"/> // Database version <meta-data Android :name="VERSION"
            android:value="2"/> // Whether SugarORM records are allowedlog 
        <meta-data
            android:name="QUERY_LOG"
            android:value="${DB_LOGGER}"<meta-data android:name= <meta-data android:name="DOMAIN_PACKAGE_NAME"
            android:value="The folder where your entity class is located." />
Copy the code

3. Create a database entity class

The skuId in the Goods table is not the default sku_id, but your own sku_id. @expoes is a annotation from Gson. As we’ll see later, the @ignore annotation emphasizes that this attribute does not create a corresponding field in the table

public class SongCollection extends SugarRecord implements Serializable {
    private long songId;
    private String songName;
    private String singerName;
    private String picUrl;
    private String playUrl;
    private long mvId;
Copy the code

The object entity needs to inherit SugarRecord and complete the GET /set method to implement Serializable easily.

4. Related operations – add, delete, modify and check

Here I use a locking singleton to manage the operation

public class SongCollectionManager {
    private static SongCollectionManager instance;

    private SongCollectionManager() {

    }

    public static SongCollectionManager getInstance() {
        if (instance == null) {
            synchronized (SongCollectionManager.class) {
                if(instance == null) { instance = new SongCollectionManager(); }}}return instance;
    }
Copy the code
  • Add the object name directly by calling the save() method
public void save(SongCollection songCollection) { synchronized (Cst.DB_LOCK) { songCollection.save(); }}Copy the code
  • Delete 1. Call delete() to delete a data object name
public void delete(long songId) { synchronized (Cst.DB_LOCK) { SongCollection songCollection = SongCollection.findById(SongCollection.class, songId); songCollection.delete(); }}Copy the code

2. Delete all entries in the table

SongCollection.deleteAll(SongCollection.class)
Copy the code
  • Modify the related attributes, and then save the data again to achieve the effect of the modification
 SongCollection songCollection = SongCollection.findById(SongCollection.class, songId);
songCollection .setSingerName("Rice");
songCollection .save();
Copy the code
  • Lookup returns a list through a conditional query
    public List<SongCollection> list() {
        synchronized (Cst.DB_LOCK) {
            List<SongCollection> list = Select.from(SongCollection.class).orderBy("id desc").list();
            if (list == null) {
                list = new ArrayList<>();
            }
            returnlist; }}Copy the code
  • The project needs to determine whether an object exists, so this method is written and posted together
public boolean find(long songId) {
        synchronized (Cst.DB_LOCK) {
            SongCollection songCollection = SongCollection.findById(SongCollection.class, songId);
            if(songCollection ! = null) {return true;
            } else {
                return false; }}}Copy the code
  • The total number of statistics
 private int count() {
        synchronized (Cst.DB_LOCK) {
            int size = (int) SongCollection.count(SongCollection.class);
            returnsize; }}Copy the code

Special reminder, knock on the blackboard ~

You can see here that the SugarRecord object has an ID of type Long. If your returned data object also has an ID, this ID is a self-growing field that can be used in ascending or descending order, but this ID can also be used directly. The GET /set method is also provided. You need to reappend a field as the primary key and sort it.