大家好,我是你的好朋友思创斯。网站金沙1005首页:https://ispacesoft.com
鸿蒙关系数据库是适合在移动设备上使用的轻量型关系型数据库,速度快,占用空间小.与轻量数据库适合存储少量简单类型数据相反,关系数据库适合存储大量复杂类型的数据.
- 拷贝数据库相关依赖jar到工程目录下
- 文件:orm_annotations_java.jar和orm_annotations_processor_java.jar
存放目录样例:d:\program files\huawei\sdk\java\3.0.0.0\build-tools\lib
- 拷贝到工程entry目录下libs里
- 修改工程的build.gradle文件
添加如下行:
annotationprocessor files("./libs/orm_annotations_java.jar", "./libs/orm_annotations_processor_java.jar")
3. 添加数据库对象定义:ormuser.java
@entity(tablename = "ormuser")
public class ormuser extends ormobject {
@primarykey(autogenerate = true)
private int id;
private int userid;
private string username;
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public int getuserid() {
return userid;
}
public void setuserid(int userid) {
this.userid = userid;
}
public string getusername() {
return username;
}
public void setusername(string username) {
this.username = username;
}
}
- 添加数据库操作对象定义:ormdbdemo.java
@database(entities = {ormuser.class}, version = 1)
public abstract class ormdbdemo extends ormdatabase {
}
- 修改slice代码:mainabilityslice.java
public class mainabilityslice extends abilityslice {
private ormcontext ormcontext;
@override
public void onstart(intent intent) {
super.onstart(intent);
super.setuicontent(resourcetable.layout_ability_main);
findcomponentbyid(resourcetable.id_writetext).setclickedlistener(component -> write());
findcomponentbyid(resourcetable.id_readtext).setclickedlistener(component -> read());
findcomponentbyid(resourcetable.id_modifytext).setclickedlistener(component -> modify());
findcomponentbyid(resourcetable.id_deltext).setclickedlistener(component -> del());
initdb();
}
private void initdb() {
databasehelper databasehelper = new databasehelper(this);
ormcontext = databasehelper.getormcontext("ormdbtest", "ormdbtest.db", ormdbdemo.class);
}
private void write() {
int userid = 1;
ormuser ormuser = new ormuser();
ormuser.setuserid(userid);
ormuser.setusername("花生皮编程");
ormcontext.insert(ormuser);
ormcontext.flush();
}
private void read() {
list<ormuser> ormusers = query();
new toastdialog(getcontext()).settext(ormusers.get(0).getusername()).show();
}
private list<ormuser> query() {
//查询userid = 1的数据
int userid = 1;
ormpredicates ormpredicates = ormcontext.where(ormuser.class).equalto("userid", userid);
return ormcontext.query(ormpredicates);
}
private void modify() {
//将查询出来的数据值修改后更新到数据库
ormuser ormuser = query().get(0);
if (ormuser == null) {
return;
}
ormuser.setusername("花生皮編程2");
ormcontext.update(ormuser);
ormcontext.flush();
}
private void del() {
//将查询出来的第一条数据从数据库中删除
ormuser ormuser = query().get(0);
if (ormuser == null) {
return;
}
ormcontext.delete(ormuser);
ormcontext.flush();
}
}
- 对应页面布局文件:
<directionallayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical">
<text ohos:id="$ id:writetext" ohos:height="match_content" ohos:width="match_content" ohos:text="写数据" ohos:text_size="20fp"/>
<text ohos:id="$ id:readtext" ohos:height="match_content" ohos:width="match_content" ohos:text="读数据" ohos:text_size="20fp"/>
<text ohos:id="$ id:modifytext" ohos:height="match_content" ohos:width="match_content" ohos:text="修改数据" ohos:text_size="20fp"/>
<text ohos:id="$ id:deltext" ohos:height="match_content" ohos:width="match_content" ohos:text="删除数据" ohos:text_size="20fp"/>
directionallayout>
ps: 上述代码已包含数据库常见的增删改查功能,代码已优化至最简。
完整源代码
https://gitee.com/hspbc/harmonyos_demos/tree/master/dbdemo
厦门大学计算机专业 | 前华为工程师
专注《》,包含:java | 安卓 | 前端 | flutter | ios | 小程序 | 鸿蒙
全网可关注:花生皮编程
文章由思创斯整理,转载请注明出处:https://ispacesoft.com/4655.html