上一篇文章写的是用一般方式对数据库进行增删改查,这篇文章还是针对用更简单的方式实现对Android轻量级数据库的增删改查。
//导入Room的支持
def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
//一张表(主键唯一 自动增长 name,age)
@Entity
public class Student {
//主键唯一 自动增长
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
private int age;
//含参构造函数
public Student( String name, int age) {
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
@Dao//Database access object == 对表进行 增删改查
public interface StudentDao {
//增
@Insert
void insertStudents(Student ... students);//插入可变参数
//条件删除
@Delete
void deleteStudents(Student ... students);
//删除 所有 @Delete 表示单个条件删除
@Query("DELETE FROM Student")
void deleteAllStudent();
//改
@Update
void updateStudents(Student ... students);
//查询所有
@Query("SELECT * FROM Student ORDER BY ID DESC")
List getAllStudent();
}
//表和对数据库的增删改查进行关联
@Database(entities = {Student.class}, version = 1, exportSchema = false)
public abstract class StudentDatabase extends RoomDatabase {
//用户只需要操作Dao,dao被用户拿到后用户就能对数据库进行增删改查了
public abstract StudentDao getStudentDao();
//单例模式 返回DB
private static StudentDatabase INSTANCE;
public static synchronized StudentDatabase getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder
(context.getApplicationContext(), StudentDatabase.class, "student_database")
//如果想玩数据库默认是异步线程
//慎用,强制开启主线程也可以操作,测试可用,真实环境下不要用
//.allowMainThreadQueries()
.build();
}
return INSTANCE;
}
@NonNull
@Override
protected SupportSQLiteOpenHelper createOpenHelper(DatabaseConfiguration config) {
return null;
}
@NonNull
@Override
protected InvalidationTracker createInvalidationTracker() {
return null;
}
@Override
public void clearAllTables() {
}
}
//DB的引擎
public class DBEngine {
//拿到Dao(对数据库的增删改查)
private StudentDao studentDao;
//完全初始化dao
public DBEngine(Context context) {
StudentDatabase studentDatabase = StudentDatabase.getInstance(context);
studentDao = studentDatabase.getStudentDao();
}
//Dao增删改查
//insert插入
public void insertStudents(Student... students) {
new InsertAsyncTask(studentDao).execute(students);
}
//update更新
public void updateStudents(Student... students) {
new UpdateAsyncTask(studentDao).execute(students);
}
//delete 删除(条件删除)
public void deleteStudents(Student... students) {
new DeleteAsyncTask(studentDao).execute(students);
}
//delete 删除(全部删除)
public void deleteAllStudents() {
new DeleteAllAsyncTask(studentDao).execute();
}
//查询全部
public void queryAllStudent() {
new queryAllAsyncTask(studentDao).execute();
}
//如果我们想玩数据库 默认是异步线程 **** 异步操作
//insert插入
static class InsertAsyncTask extends AsyncTask {
private StudentDao dao;
public InsertAsyncTask(StudentDao studentDao) {
dao = studentDao;
}
@Override
protected Void doInBackground(Student... students) {
dao.insertStudents(students);
return null;
}
}
//update更新
static class UpdateAsyncTask extends AsyncTask {
private StudentDao dao;
public UpdateAsyncTask(StudentDao studentDao) {
dao = studentDao;
}
@Override
protected Void doInBackground(Student... students) {
dao.updateStudents(students);
return null;
}
}
//delete 删除(条件删除)
static class DeleteAsyncTask extends AsyncTask {
private StudentDao dao;
public DeleteAsyncTask(StudentDao studentDao) {//既然传递了steudent进来 就是有条件删除
dao = studentDao;
}
@Override
protected Void doInBackground(Student... students) {
dao.deleteStudents(students);
return null;
}
}
//delete 删除(全部删除)
static class DeleteAllAsyncTask extends AsyncTask {
private StudentDao dao;
public DeleteAllAsyncTask(StudentDao studentDao) {//既然传递了steudent进来 就是有条件删除
dao = studentDao;
}
@Override
protected Void doInBackground(Void... voids) {
dao.deleteAllStudent();
return null;
}
}
//全部查询
static class queryAllAsyncTask extends AsyncTask {
private StudentDao dao;
public queryAllAsyncTask(StudentDao studentDao) {
dao = studentDao;
}
@Override
protected Void doInBackground(Void... voids) {
List allStudent = dao.getAllStudent();
//遍历全部查询的结果(使用迭代查询)
for (Student student : allStudent) {
Log.e("Anglin", "doInBackground: 全部查询的每一项" + student.toString());
}
return null;
}
}
}
public class RoomActivity extends AppCompatActivity {
private DBEngine dbEngine;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room);
dbEngine = new DBEngine(this);
}
/**
* 插入
* @param view
*/
public void insertAction(View view) {
Student student1 = new Student("张三", 20);
Student student2 = new Student("李四", 22);
Student student3 = new Student("王五", 34);
dbEngine.insertStudents(student1,student2,student3);
}
/**
* 修改 下标为3
* @param view
*/
public void updateAction(View view) {
Student student = new Student("朱文静", 24);
student.setId(27);//要找到下标
dbEngine.updateStudents(student);
}
/**
* 删除(条件删除)删除下标为3
* @param view
*/
public void deleteAction(View view) {
Student student =new Student(null,0);
student.setId(27);
dbEngine.deleteStudents(student);
}
/**
* 全部 查询
* @param view
*/
public void queryAction(View view) {
dbEngine.queryAllStudent();
}
/**
* 删除所有
* @param view
*/
public void DeleteAllAction(View view) {
dbEngine.deleteAllStudents();
}
}