当前位置:网站首页>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"));边栏推荐
- Use of snippets in vscode (code template)
- Introduction to memory layout of FVP and Juno platforms
- YOLOv5-Shufflenetv2
- Research on the value of background repeat of background tiling
- Transport connection management of TCP
- 2022/7/1学习总结
- Solon 框架如何方便获取每个请求的响应时间?
- FVP和Juno平台的Memory Layout介绍
- 2022/7/1學習總結
- 搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
猜你喜欢

Unity check whether the two objects have obstacles by ray

C language Essay 1

UE fantasy engine, project structure

Page countdown
![[interval problem] 435 Non overlapping interval](/img/a3/2911ee72635b93b6430c2efd05ec9a.jpg)
[interval problem] 435 Non overlapping interval
![[转]: OSGI规范 深入浅出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[转]: OSGI规范 深入浅出
![[trans]: spécification osgi](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[trans]: spécification osgi

Binary search basis

Data is stored in the form of table
![[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
随机推荐
PMP candidates, please check the precautions for PMP examination in July
Embedded database development programming (V) -- DQL
嵌入式数据库开发编程(六)——C API
[speed pointer] 142 circular linked list II
[leetcode] integer inversion [7]
[allocation problem] 455 Distribute cookies
Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!
Pointnet++学习
Bubble sort summary
Do a small pressure test with JMeter tool
win下一键生成当日的时间戳文件
2022/7/1學習總結
Time format conversion
Introduction to tools in TF-A
软件测试 -- 0 序
FVP和Juno平台的Memory Layout介绍
Magnifying glass effect
Demonstration of using Solon auth authentication framework (simpler authentication framework)
How can the Solon framework easily obtain the response time of each request?
支持多模多态 GBase 8c数据库持续创新重磅升级