First, the use of Room involves three foot colors

1. Create a table

package com.lzl.jectpackdemo; import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.PrimaryKey; @Entity public class Student { @PrimaryKey(autoGenerate = true) private int uid; @ColumnInfo(name = "name") private String name; @ColumnInfo(name = "pwd") private String pwd; @ColumnInfo(name = "address") private String address; public Student(String name, String pwd, String address) { this.name = name; this.pwd = pwd; this.address = address; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Student{" + "uid=" + uid + ", name='" + name + ''' + ", pwd='" + pwd + ''' + ", address='" + address + ''' + '}'; }}Copy the code

2. Create the Dao

/** * Use apt technology to generate implementation classes. */ @androidx.room.Dao public interface Dao { @Insert void insert(Student... students); @Delete void delete(Student student); @Update void updata(Student student); @Query("select * from Student") List<Student> getAll(); @Query("select * from Student where name like:name") Student findByName(String name); }Copy the code

3. Create a database

@Database(entities = {Student.class},version = 1,exportSchema = false) public abstract class DataBase extends RoomDatabase {// expose dao public abstract DAO userDao(); }Copy the code

Two, use method

new Thread(new Runnable() { @Override public void run() { DataBase dataBase = Room.databaseBuilder(getApplicationContext(), DataBase.class,"MyDb").build(); Dao dao = dataBase.userDao(); dao.insert(new Student("TIANLONG","123","....." )); dao.insert(new Student("TIANLONG1","123","....." )); dao.insert(new Student("TIANLONG2","123","....." )); List<Student> all = dao.getAll(); Log.e("lzl",all.toString()); } }).start();Copy the code

3. Use DataBase Navigator to view the DataBase

1. Download the plug-in through the plugin

2. If you can’t find it here, you can download it offline

Plugins.jetbrains.com/plugin/1800…

Download directly after the compression package can be added, restart can use.

3. Download the database files from data/data/ package name /database to the local PC.

Note The path must be in English; otherwise, the connection fails

4. How to use the plugin Database Navigator, follow the figure.

4. Architecture of MVVM