当前位置:网站首页>room数据库的使用
room数据库的使用
2022-07-05 05:13:00 【菜鸟xiaowang】
1.导包
implementation "androidx.room:room-runtime:2.4.2" annotationProcessor "androidx.room:room-compiler:2.4.2"
2.gradle中配置
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}3.创建数据对象实体
@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.创建对应的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.创建数据库
//exportSchema = true 支持导出Room生成的配置文件
@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.调用
MyRoomDatabase.getInstance(this).getUserDao().addUser(new User("wang","lllll"));边栏推荐
- China polyurethane rigid foam Market Research and investment forecast report (2022 Edition)
- Cocos2dx screen adaptation
- BUUCTF MISC
- When will Wei Lai, who has been watched by public opinion, start to "build high-rise buildings" again?
- Establish cloth effect in 10 seconds
- 【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
- 中国聚氨酯硬泡市场调研与投资预测报告(2022版)
- Autocad-- Real Time zoom
- Unity sends messages and blocks indecent words
- 《动手学深度学习》学习笔记
猜你喜欢

win10虚拟机集群优化方案

【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research

Recherche de mots pour leetcode (solution rétrospective)

UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化

嵌入式数据库开发编程(五)——DQL

小程序直播+電商,想做新零售電商就用它吧!

PostgreSQL 超越 MySQL,“世界上最好的编程语言”薪水偏低

AutoCAD - lengthening

Optimization scheme of win10 virtual machine cluster

《动手学深度学习》学习笔记
随机推荐
Research on the value of background repeat of background tiling
2022/7/1 learning summary
Basic knowledge points of dictionary
Research and investment forecast report of adamantane industry in China (2022 Edition)
Stm32cubemx (8): RTC and RTC wake-up interrupt
A three-dimensional button
Es module and commonjs learning notes -- ESM and CJS used in nodejs
The next key of win generates the timestamp file of the current day
3dsmax scanning function point connection drawing connection line
Chinese notes of unit particle system particle effect
Reverse one-way linked list of interview questions
AutoCAD - window zoom
AutoCAD - set layer
54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
Django reports an error when connecting to the database. What is the reason
Lua GBK and UTF8 turn to each other
AutoCAD - command repetition, undo and redo
AutoCAD - full screen display
Unity writes timetables (without UI)