当前位置:网站首页>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"));边栏推荐
- 【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
- [sum of two numbers] 169 sum of two numbers II - enter an ordered array
- Development error notes
- 小程序直播+電商,想做新零售電商就用它吧!
- 十年不用一次的JVM调用
- win10虚拟机集群优化方案
- [binary search] 69 Square root of X
- [turn]: Apache Felix framework configuration properties
- object serialization
- [turn to] MySQL operation practice (III): table connection
猜你喜欢

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

利用HashMap实现简单缓存
![[depth first search] 695 Maximum area of the island](/img/08/cfff4aec667216e4f146205a12c13f.jpg)
[depth first search] 695 Maximum area of the island

Reverse one-way linked list of interview questions

Generate filled text and pictures

2022/7/2做题总结

YOLOv5-Shufflenetv2

《动手学深度学习》学习笔记
![[to be continued] [UE4 notes] L2 interface introduction](/img/0f/268c852b691bd7459785537f201a41.jpg)
[to be continued] [UE4 notes] L2 interface introduction

Binary search basis
随机推荐
C语言杂谈1
object serialization
[转]: OSGI规范 深入浅出
Learning notes of "hands on learning in depth"
利用HashMap实现简单缓存
Haut OJ 1347: addition of choice -- high progress addition
[转]MySQL操作实战(三):表联结
Listview is added and deleted at the index
A complete attack chain
使用命令符关闭笔记本自带键盘命令
cocos_ Lua loads the file generated by bmfont fnt
The next key of win generates the timestamp file of the current day
Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
一个新的微型ORM开源框架
Solon Auth 认证框架使用演示(更简单的认证框架)
Cocos2dx screen adaptation
Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area
Kali 2018 full image download
Data is stored in the form of table