当前位置:网站首页>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 to] MySQL operation practice (I): Keywords & functions
- Transport connection management of TCP
- Unity card flipping effect
- Haut OJ 1357: lunch question (I) -- high precision multiplication
- What is the agile proportion of PMP Exam? Dispel doubts
- When will Wei Lai, who has been watched by public opinion, start to "build high-rise buildings" again?
- Simple HelloWorld color change
- On-off and on-off of quality system construction
- Haut OJ 1321: mode problem of choice sister
- [allocation problem] 135 Distribute candy
猜你喜欢

YOLOv5添加注意力机制

object serialization
![[to be continued] [UE4 notes] L1 create and configure items](/img/20/54ba719be2e51b7db5b7645b361e26.jpg)
[to be continued] [UE4 notes] L1 create and configure items

Grail layout and double wing layout

Django reports an error when connecting to the database. What is the reason

National teacher qualification examination in the first half of 2022

Introduction to tools in TF-A

Optimization scheme of win10 virtual machine cluster
![[转]MySQL操作实战(三):表联结](/img/70/20bf9b379ce58761bae9955982a158.png)
[转]MySQL操作实战(三):表联结
![[depth first search] 695 Maximum area of the island](/img/08/cfff4aec667216e4f146205a12c13f.jpg)
[depth first search] 695 Maximum area of the island
随机推荐
2022/7/1学习总结
Pause and resume of cocos2dx Lua scenario
Solon 框架如何方便获取每个请求的响应时间?
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
Development error notes
Es module and commonjs learning notes
Time format conversion
Embedded database development programming (zero)
Unity ugui source code graphic
Haut OJ 1352: string of choice
[binary search] 69 Square root of X
Research on the value of background repeat of background tiling
Bucket sort
Introduction to memory layout of FVP and Juno platforms
[转]: OSGI规范 深入浅出
Magnifying glass effect
A complete attack chain
Listview pull-down loading function
Haut OJ 1321: mode problem of choice sister
Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area