时间:2023-04-10 20:53:32来源:本站整理作者:点击:
鸿蒙关系数据库是适合在移动设备上使用的轻量型关系型数据库,速度快,占用空间小.与轻量数据库适合存储少量简单类型数据相反,关系数据库适合存储大量复杂类型的数据.
基础样例
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;
}
}
@Database(entities = {OrmUser.class}, version = 1)
public abstract class OrmDBDemo extends OrmDatabase {
}
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();
}
}
<?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">
<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
,伊利QQ星奶粉和小小CBA达成战略合作,2023年榛高篮球挑战赛全面开启
父母家暴对孩子的影响,如何处理夫妻感情中的家庭暴力?父母家暴对孩子的影响,如何处理夫妻感情中的家庭暴力?
夫妻吵架总冷战怎么办?怎么和好并且化解夫妻吵架呢?林志玲登上11月杂志封面,做妈妈之后成为幸福的代名词“关公”陆树铭因病去世,网爆大衣哥亲往西安悼念,被赞有情有义《卿卿日常》24节气姑娘结局是什么_《卿卿日常》24节气姑娘离开老三了吗Copyright 2022-2026 feiyundao.com 〖妃孕岛〗 版权所有 陕ICP备2022000637号-4
声明: 本站文章均来自互联网,不代表本站观点 如有异议 请与本站联系 本站为非赢利性网站 不接受任何赞助和广告