当前位置:网站首页>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"));
边栏推荐
- Unity enables mobile phone vibration
- 被舆论盯上的蔚来,何时再次“起高楼”?
- Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area
- How to choose a panoramic camera that suits you?
- Cocos create Jiugongge pictures
- UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存
- Simple modal box
- AutoCAD - scaling
- Lua determines whether the current time is the time of the day
- Magnifying glass effect
猜你喜欢
Leetcode word search (backtracking method)
LeetCode之单词搜索(回溯法求解)
Learning notes of "hands on learning in depth"
Autocad-- Real Time zoom
Quick sort summary
Create a pyGame window with a blue background
Magnifying glass effect
UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
Optimization scheme of win10 virtual machine cluster
随机推荐
嵌入式数据库开发编程(五)——DQL
54. Spiral matrix & 59 Spiral matrix II ●●
Pdf to DWG in CAD
Grail layout and double wing layout
2022/7/2做题总结
Merge sort
Simple HelloWorld color change
2020-10-27
Chinese notes of unit particle system particle effect
Dotween usage records ----- appendinterval, appendcallback
C4D simple cloth (version above R21)
十年不用一次的JVM调用
3dsmax scanning function point connection drawing connection line
Research on the value of background repeat of background tiling
django连接数据库报错,这是什么原因
Lua determines whether the current time is the time of the day
AutoCAD - full screen display
Unity card flipping effect
Cocos create Jiugongge pictures
54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●