当前位置:网站首页>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"));边栏推荐
- [turn]: Apache Felix framework configuration properties
- How can the Solon framework easily obtain the response time of each request?
- A three-dimensional button
- Research on the value of background repeat of background tiling
- A complete attack chain
- Listview is added and deleted at the index
- 服务熔断 Hystrix
- Bucket sort
- Solon 框架如何方便获取每个请求的响应时间?
- 2022年上半年国家教师资格证考试
猜你喜欢
![To be continued] [UE4 notes] L4 object editing](/img/0f/cfe788f07423222f9eed90f4cece7d.jpg)
To be continued] [UE4 notes] L4 object editing
![[轉]: OSGI規範 深入淺出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[轉]: OSGI規範 深入淺出

TF-A中的工具介绍

【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
![[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research](/img/17/db8614b177f33ee4f67b7d65a8430f.png)
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research

win10虚拟机集群优化方案

嵌入式数据库开发编程(零)
![[trans]: spécification osgi](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[trans]: spécification osgi

服务熔断 Hystrix

Collapse of adjacent vertical outer margins
随机推荐
2022/7/1學習總結
Download and use of font icons
软件测试 -- 0 序
Page countdown
小程序直播+电商,想做新零售电商就用它吧!
Improvement of pointnet++
JVM call not used once in ten years
Simple HelloWorld color change
[turn]: Apache Felix framework configuration properties
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
2022/7/1 learning summary
[speed pointer] 142 circular linked list II
[转]MySQL操作实战(一):关键字 & 函数
Shell Sort
[to be continued] [UE4 notes] L1 create and configure items
Yolov5 adds attention mechanism
[allocation problem] 135 Distribute candy
Haut OJ 1241: League activities of class XXX
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
Haut OJ 1221: a tired day