当前位置:网站首页>Use of room database
Use of room database
2022-07-05 05:19:00 【Novice Xiaowang】
1. Guide pack
implementation "androidx.room:room-runtime:2.4.2" annotationProcessor "androidx.room:room-compiler:2.4.2"
2.gradle Middle configuration
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}3. Create data object entity
@Entity
public class User {
@PrimaryKey(autoGenerate = true)
long userId;
@ColumnInfo(name = "username")
String userName;
@ColumnInfo(name = "password")
String password;
public User(@NonNull String userName,@NonNull String password){
this.userName = userName;
this.password = password;
}
}4. Create the corresponding Dao
@Dao
public interface UserDao {
@Insert
void addUser(User ... user);
@Delete
int deleteUser(User user);
@Update
int updateUser(User user);
@Query("SELECT * FROM user WHERE userID = :id")
User queryUser(long id);
}5. Create database
//exportSchema = true Support export Room Generated profile
@Database(entities = {User.class},version = 3,exportSchema = true)
public abstract class MyRoomDatabase extends RoomDatabase {
public static String roomName = "user";
public static MyRoomDatabase instance;
public abstract UserDao getUserDao();
public static MyRoomDatabase getInstance(Context context){
if (instance == null){
synchronized (MyRoomDatabase.class){
if (instance == null){
instance = Room.databaseBuilder(context,MyRoomDatabase.class,roomName).allowMainThreadQueries().build();
}
}
}
return instance;
}
}6. call
MyRoomDatabase.getInstance(this).getUserDao().addUser(new User("wang","lllll"));边栏推荐
- How can the Solon framework easily obtain the response time of each request?
- 2022/7/2 question summary
- Collapse of adjacent vertical outer margins
- Web APIs DOM节点
- 2022/7/1 learning summary
- YOLOv5添加注意力機制
- The next key of win generates the timestamp file of the current day
- Do a small pressure test with JMeter tool
- Haut OJ 1350: choice sends candy
- room数据库的使用
猜你喜欢
随机推荐
Embedded database development programming (zero)
PMP考生,请查收7月PMP考试注意事项
[binary search] 34 Find the first and last positions of elements in a sorted array
Quick sort summary
[turn to] MySQL operation practice (III): table connection
2022/7/2做题总结
Listview pull-down loading function
《动手学深度学习》学习笔记
JVM call not used once in ten years
2022/7/1 learning summary
Embedded database development programming (V) -- DQL
Django reports an error when connecting to the database. What is the reason
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
[allocation problem] 455 Distribute cookies
小程序直播+电商,想做新零售电商就用它吧!
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
win10虚拟机集群优化方案
Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
[转]: OSGI规范 深入浅出
Page countdown



![[转]MySQL操作实战(三):表联结](/img/70/20bf9b379ce58761bae9955982a158.png)
![To be continued] [UE4 notes] L4 object editing](/img/0f/cfe788f07423222f9eed90f4cece7d.jpg)
![[轉]: OSGI規範 深入淺出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)


![[merge array] 88 merge two ordered arrays](/img/e9/a73d9f22eead8e68c1e45c27ff6e6c.jpg)
