当前位置:网站首页>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"));
边栏推荐
- [to be continued] [depth first search] 547 Number of provinces
- Romance of programmers on Valentine's Day
- Bucket sort
- win10虚拟机集群优化方案
- [merge array] 88 merge two ordered arrays
- xftp7与xshell7下载(官网)
- Yolov5 adds attention mechanism
- Django reports an error when connecting to the database. What is the reason
- Collapse of adjacent vertical outer margins
- Haut OJ 1221: a tired day
猜你喜欢
Embedded database development programming (zero)
十年不用一次的JVM调用
Learning notes of "hands on learning in depth"
JVM call not used once in ten years
[speed pointer] 142 circular linked list II
[turn]: OSGi specification in simple terms
[转]MySQL操作实战(三):表联结
嵌入式数据库开发编程(六)——C API
Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
[转]MySQL操作实战(一):关键字 & 函数
随机推荐
Reverse one-way linked list of interview questions
2022/7/1学习总结
A three-dimensional button
Haut OJ 1245: large factorial of CDs --- high precision factorial
2022 / 7 / 1 Résumé de l'étude
Unity ugui source code graphic
Haut OJ 1218: maximum continuous sub segment sum
Use of snippets in vscode (code template)
Unity enables mobile phone vibration
Listview is added and deleted at the index
Programmers' experience of delivering takeout
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
A complete attack chain
Web APIs DOM节点
Stm32cubemx (8): RTC and RTC wake-up interrupt
Haut OJ 1321: mode problem of choice sister
Cocos progress bar progresstimer
[转]: OSGI规范 深入浅出
Magnifying glass effect
Listview pull-down loading function