---
# System prepended metadata

title: Android Room Library
tags: [Android]

---

# Android Room Library
###### tags: `Android`

**Dashboard use Android Room Library to implement database feature**

[reference]
```
https://developer.android.com/topic/libraries/architecture/room Room Persistence Library
```

## Introduction

Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

There are 3 major components in Room:

* Database
    * Contains the database holder and serves as the main access point for the underlying connection to your app's persisted, relational data.
    * The class that's annotated with '''@Database''' should satisfy the following conditions:
    * Be an abstract class that extends '''RoomDatabase'''.
    * Include the list of entities associated with the database within the annotation.
    * Contain an abstract method that has 0 arguments and returns the class that is annotated with '''@Dao'''.
    * At runtime, you can acquire an instance of Database by calling '''Room.databaseBuilder()''' or '''Room.inMemoryDatabaseBuilder()'''.

* Entity
    * Represents a table within the database.

* DAO
    * Contains the methods used for accessing the database.


This relationship among the different components of Room appears:

![](https://i.imgur.com/LITxBac.png)


## Implement

The following code snippet contains a sample database configuration with one entity and one DAO:


* User
```
 @Entity
public class User {
    @PrimaryKey
    public int uid;

    @ColumnInfo(name = "first_name")
    public String firstName;

    @ColumnInfo(name = "last_name")
    public String lastName;
}
```

* UserDao
```
 @Dao
public interface UserDao {
    @Query("SELECT * FROM user")
    List<User> getAll();

    @Query("SELECT * FROM user WHERE uid IN (:userIds)")
    List<User> loadAllByIds(int[] userIds);

    @Query("SELECT * FROM user WHERE first_name LIKE :first AND " +
           "last_name LIKE :last LIMIT 1")
    User findByName(String first, String last);

    @Insert
    void insertAll(User... users);

    @Delete
    void delete(User user);
}
```

## Using in QIoT

QIoT use two tables to save data:


* Dashboad
```
 @Entity(tableName = "Dashboard")
public class Dashboard implements Serializable {

    @PrimaryKey(autoGenerate = true)
    private int id;
    private String title;
    private int pin;
    private int isShared;
    private String user;
    private int errorCode;
}
```

* WidgetInfo
```
 @Entity(tableName = "WidgetInfo",
        foreignKeys = {
                @ForeignKey(entity = Dashboard.class,
                        parentColumns = "id",
                        childColumns = "dashboardId",
                        onDelete = ForeignKey.CASCADE)},
        indices = {@Index(value = "dashboardId")
        })
public class WidgetInfo implements Serializable, AppConstants {
    @PrimaryKey(autoGenerate = true)
    private int id;
    private int dashboardId;
    private String widgetType;
    private String name;
    private String host;
    private int port;
    private String username;
    private String password;
    private String clientId;
    private String nasUniqueId;
    private int resourceType;
    private String thingId;
    private String thingName;
    private String resourceId;
    private String resourceName;
    private String resourceObjectId;
    private String topic;
    private ArrayList<String> keyValueList;
    private ArrayList<String> labelList;
    private ArrayList<Payload> payloadList;
    private String unit;
    private int chartStyle;
    private int minValue;
    private int maxValue;
    private String videoUrl;
    private String videoUsername;
    private String videoPassword;
    private boolean showMyLocation;
    private String longitude = "";
    private String latitude;
    private String timeType;
    private String startTime;
    private String endTime;
    private ElementLayoutParams elementLayoutParams;
    private String color;
    private String notificationId;
    private String condition;
    private int threshold;
    private int errorCode;
}
```
