HarmonyOS 基础数据库
1.配置
2.使用
一 配置
javapoet_java.jar
orm_annotations_java.jar
orm_annotations_processor_java.jar
这三个包拷贝到项目libs目录后配置2个地方。
compileOptions {
annotationEnabled true
}
compile files(
"./libs/javapoet_java.jar",
"./libs/orm_annotations_java.jar",
"./libs/orm_annotations_processor_java.jar"
)
annotationProcessor files(
"./libs/javapoet_java.jar",
"./libs/orm_annotations_java.jar",
"./libs/orm_annotations_processor_java.jar"
)
二 使用
1.新增
2.更新
3.删除
4.查询
package com.example.demo2.data; public class Student { int id; String name; int age; double salary; public Student() { } public Student(int id, String name, int age, double salary) { this.id = id; this.name = name; this.age = age; this.salary = salary; } 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; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}'; } }
package com.example.demo2.utils; import com.example.demo2.data.Student; import ohos.app.Context; import ohos.data.DatabaseHelper; import ohos.data.rdb.*; import ohos.data.resultset.ResultSet; import java.util.ArrayList; import java.util.List; public class DBUtils { private RdbStore rdbStore; private DatabaseHelper databaseHelper; public DBUtils(Context context) { databaseHelper = new DatabaseHelper(context); initDB(); } private void initDB() { StoreConfig storeConfig = StoreConfig.newDefaultConfig("school.db"); rdbStore = databaseHelper.getRdbStore(storeConfig, 1, new RdbOpenCallback() { @Override public void onCreate(RdbStore rdbStore) { rdbStore.executeSql("create table if not exists " + "student(id integer primary key autoincrement," + "name text not null,age integer,salary real)"); } @Override public void onUpgrade(RdbStore rdbStore, int i, int i1) { } }); } public boolean insertStudent(Student student) { ValuesBucket valuesBucket = new ValuesBucket(); valuesBucket.putInteger("age", student.getAge()); valuesBucket.putString("name", student.getName()); valuesBucket.putDouble("salary", student.getSalary()); long insert = rdbStore.insert("student", valuesBucket); return insert > 0 ? true : false; } public boolean updateStudent(Student student) { ValuesBucket valuesBucket = new ValuesBucket(); valuesBucket.putInteger("age", student.getAge()); valuesBucket.putString("name", student.getName()); valuesBucket.putDouble("salary", student.getSalary()); RdbPredicates rdbPredicates = new RdbPredicates("student"); rdbPredicates.equalTo("id", student.getId()); int t = rdbStore.update(valuesBucket, rdbPredicates); return t > 0 ? true : false; } public boolean deleteStudent(Student student) { RdbPredicates rdbPredicates = new RdbPredicates("student"); rdbPredicates.equalTo("id", student.getId()); int t = rdbStore.delete(rdbPredicates); return t > 0 ? true : false; } public Student queryById(int id) { Student student = new Student(); RdbPredicates rdbPredicates = new RdbPredicates("student"); rdbPredicates.equalTo("id", id); ResultSet query = rdbStore.query(rdbPredicates, new String[]{"id", "name", "age", "salary"}); while (query.goToNextRow()) { student.setId(query.getInt(query.getColumnIndexForName("id"))); student.setName(query.getString(query.getColumnIndexForName("name"))); student.setAge(query.getInt(query.getColumnIndexForName("age"))); student.setSalary(query.getDouble(query.getColumnIndexForName("salary"))); } return student; } public List<Student> queryList(String name) { List<Student> list = new ArrayList<Student>(); RdbPredicates rdbPredicates = new RdbPredicates("student"); rdbPredicates.like("name", name); ResultSet query = rdbStore.query(rdbPredicates, new String[]{"id", "name", "age", "salary"}); while (query.goToNextRow()) { Student student = new Student(); student.setId(query.getInt(query.getColumnIndexForName("id"))); student.setName(query.getString(query.getColumnIndexForName("name"))); student.setAge(query.getInt(query.getColumnIndexForName("age"))); student.setSalary(query.getDouble(query.getColumnIndexForName("salary"))); list.add(student); } return list; } }
package com.example.demo2.slice; import com.example.demo2.ResourceTable; import com.example.demo2.data.Student; import com.example.demo2.utils.DBUtils; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Button; import ohos.agp.components.Component; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; import java.util.List; public class DBTestSlice extends AbilitySlice { private final HiLogLabel hiLogLabel = new HiLogLabel(HiLog.LOG_APP, 1, "DBTestSlice"); @Override protected void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_db_test); Button add = (Button) findComponentById(ResourceTable.Id_btnAdd); Button update = (Button) findComponentById(ResourceTable.Id_btnUpdate); Button delete = (Button) findComponentById(ResourceTable.Id_btnDelete); Button one = (Button) findComponentById(ResourceTable.Id_btnQueryById); Button list = (Button) findComponentById(ResourceTable.Id_btnQueryAll); add.setClickedListener(this::onClick); update.setClickedListener(this::onClick); delete.setClickedListener(this::onClick); one.setClickedListener(this::onClick); list.setClickedListener(this::onClick); } private void onClick(Component component) { if (component.getId() == ResourceTable.Id_btnAdd) { try { for (int i = 0; i < 10; i++) { Student student = new Student(i, "jack", 30 + i, 12000); boolean b = new DBUtils(DBTestSlice.this).insertStudent(student); if (b) { HiLog.info(hiLogLabel, "---插入成功-----" + i); } } } catch (Exception e) { HiLog.info(hiLogLabel, "---插入失败-----" + e.getMessage()); } } if (component.getId() == ResourceTable.Id_btnQueryById) { try { Student student = new DBUtils(DBTestSlice.this).queryById(0); HiLog.info(hiLogLabel, "---查询1条成功-----"); HiLog.info(hiLogLabel, student.getName()); } catch (Exception e) { HiLog.info(hiLogLabel, "---查询1条失败-----" + e.getMessage()); } } if (component.getId() == ResourceTable.Id_btnQueryAll) { try { List<Student> list = new DBUtils(DBTestSlice.this).queryList("jack"); HiLog.info(hiLogLabel, "---查询全部:size-----" + list.size()); for (int i = 0; i < list.size(); i++) { HiLog.info(hiLogLabel, list.get(i).toString()); } HiLog.info(hiLogLabel, "---查询全部成功-----"); } catch (Exception e) { HiLog.info(hiLogLabel, "---查询全部失败-----" + e.getMessage()); } } } @Override protected void onActive() { super.onActive(); } @Override protected void onForeground(Intent intent) { super.onForeground(intent); } }
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Button ohos:id="$+id:btnAdd" ohos:height="50vp" ohos:width="200vp" ohos:background_element="$graphic:background_button" ohos:text="新增1条数据" ohos:text_color="#FFFFFF" ohos:text_size="20fp" ohos:top_margin="20vp"> </Button> <Button ohos:id="$+id:btnDelete" ohos:height="50vp" ohos:width="200vp" ohos:background_element="$graphic:background_button" ohos:text="删除1条数据" ohos:text_color="#FFFFFF" ohos:text_size="20fp" ohos:top_margin="20vp"> </Button> <Button ohos:id="$+id:btnUpdate" ohos:height="50vp" ohos:width="200vp" ohos:background_element="$graphic:background_button" ohos:text="更新1条数据" ohos:text_color="#FFFFFF" ohos:text_size="20fp" ohos:top_margin="20vp"> </Button> <Button ohos:id="$+id:btnQueryById" ohos:height="50vp" ohos:width="200vp" ohos:background_element="$graphic:background_button" ohos:text="查询1条数据" ohos:text_color="#FFFFFF" ohos:text_size="20fp" ohos:top_margin="20vp"> </Button> <Button ohos:id="$+id:btnQueryAll" ohos:height="50vp" ohos:width="200vp" ohos:background_element="$graphic:background_button" ohos:text="查询全部数据" ohos:text_color="#FFFFFF" ohos:text_size="20fp" ohos:top_margin="20vp"> </Button> </DirectionalLayout>
天生我材必有用,千金散尽还复来