当前位置:网站首页>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"));
边栏推荐
- win10虚拟机集群优化方案
- Haut OJ 1352: string of choice
- Haut OJ 1245: large factorial of CDs --- high precision factorial
- 2022/7/1学习总结
- 服务熔断 Hystrix
- 嵌入式数据库开发编程(零)
- [to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
- sync.Mutex源码解读
- [allocation problem] 135 Distribute candy
- Quick sort summary
猜你喜欢
随机推荐
【ES实战】ES上的native realm安全方式使用
win下一键生成当日的时间戳文件
2022/7/2做题总结
FVP和Juno平台的Memory Layout介绍
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
Demonstration of using Solon auth authentication framework (simpler authentication framework)
Listview is added and deleted at the index
Unity check whether the two objects have obstacles by ray
记录QT内存泄漏的一种问题和解决方案
《动手学深度学习》学习笔记
Collapse of adjacent vertical outer margins
[to be continued] [depth first search] 547 Number of provinces
Simple modal box
Use of snippets in vscode (code template)
Double pointer Foundation
Stm32cubemx (8): RTC and RTC wake-up interrupt
Embedded database development programming (VI) -- C API
[depth first search] 695 Maximum area of the island
Data is stored in the form of table
cocos2dx_ Lua card flip