鸿蒙关系数据库代码案例
鸿蒙关系数据库代码案例单页写完便于新手入门学习// Index.ets import relationalStore from ohos.data.relationalStore; import common from ohos.app.ability.common; // 数据表常量 const TABLE_NAME user; const DB_NAME PracticeDB.db; const DB_CONFIG: relationalStore.StoreConfig { name: DB_NAME, securityLevel: relationalStore.SecurityLevel.S1 }; // 用户实体 interface User { id?: number;name: string; age: number; } Entry Component struct RdbCurdDemo { State userList: User[] []; State inputName: string ; State inputAge: number 18; rdbStore: relationalStore.RdbStore | null null; ctx: common.UIAbilityContext | null null; aboutToAppear() { this.ctx getContext(this) as common.UIAbilityContext; this.initDB(); } // 初始化数据库、创建表 async initDB() { if (!this.ctx) return; try { this.rdbStore await relationalStore.getRdbStore(this.ctx, DB_CONFIG); const createSql CREATE TABLE IF NOT EXISTS ${TABLE_NAME} ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER ); await this.rdbStore.executeSql(createSql); console.info(数据库初始化成功); this.queryAll(); } catch (err) { console.error(初始化失败, JSON.stringify(err)); } } // 【CREATE】新增 async insertUser() { if (!this.rdbStore || !this.inputName) return; const valueBucket: relationalStore.ValuesBucket { name: this.inputName, age: this.inputAge }; try { const rowId await this.rdbStore.insert(TABLE_NAME, valueBucket); console.info(新增成功 rowId:, rowId); this.queryAll(); } catch (e) { console.error(新增失败, e); } } // 【READ】查询全部修复重点使用RdbPredicates async queryAll() { if (!this.rdbStore) return; let resultSet: relationalStore.ResultSet | null null; try { // ✅ 正确写法构造查询条件对象不再直接传表名字符串 const predicates new relationalStore.RdbPredicates(TABLE_NAME); resultSet await this.rdbStore.query(predicates, [id, name, age]); const list: User[] []; // !非空断言resultSet这里一定有值 while (resultSet.goToNextRow()) { list.push({ id: resultSet.getLong(0), name: resultSet.getString(1), age: resultSet.getLong(2) }); } this.userList list; } catch (e) { console.error(查询失败, e); } finally { // 关闭游标 if (resultSet ! null) { resultSet.close(); } } } // 【UPDATE】修改 async updateUser(userId: number, newName: string) { if (!this.rdbStore) return; const bucket: relationalStore.ValuesBucket { name: newName }; const predicates new relationalStore.RdbPredicates(TABLE_NAME); predicates.equalTo(id, userId); try { const count await this.rdbStore.update(bucket, predicates); console.info(更新行数${count}); this.queryAll(); } catch (e) { console.error(更新失败, e); } } // 【DELETE】删除 async deleteUser(userId: number) { if (!this.rdbStore) return; const predicates new relationalStore.RdbPredicates(TABLE_NAME); predicates.equalTo(id, userId); try { const delCount await this.rdbStore.delete(predicates); console.info(删除行数${delCount}); this.queryAll(); } catch (e) { console.error(删除失败, e); } } build() { Column() { Text(RDB数据库 CURD 练习) .fontSize(24) .fontWeight(FontWeight.Bold) .margin({ bottom: 12 }) Row() { TextInput({ text: this.inputName, placeholder: 姓名 }) .width(120) .onChange((v: string) this.inputName v) TextInput({ text: this.inputAge.toString(), placeholder: 年龄 }) .width(80) .margin({ left: 6 }) .onChange((v: string) { const num Number(v); this.inputAge isNaN(num) ? 0 : num; }) }.margin({ bottom: 8 }) Button(新增用户) .width(90%) .onClick(() this.insertUser()) Text( 用户列表 ).margin({ top: 12 }) List() { ForEach(this.userList, (item: User) { ListItem() { Row() { Text(ID:${item.id} ${item.name} ${item.age}岁) .layoutWeight(1) Button(改名字) .fontSize(12) .margin({ right: 4 }) .onClick(() this.updateUser(item.id!, item.name _edit)) Button(删除) .fontSize(12) .backgroundColor(#dc3545) .onClick(() this.deleteUser(item.id!)) } } }) } .width(100%) .height(300) .margin({ top: 8 }) } .width(100%) .padding(16) } }

相关新闻

Unity学习全攻略:从环境搭建到性能优化的系统指南

Unity学习全攻略:从环境搭建到性能优化的系统指南

1. 项目概述:为什么我们需要一份“Awesome Unity Tutorial”指南?如果你在搜索引擎里敲下“Unity教程”这四个字,得到的海量结果可能会让你瞬间陷入选择困难。从官方文档到B站UP主的系列视频,从GitHub上的开源项目到各种付费课程&…

2026/11/16 21:12:25 阅读更多 →
Unity集成讯飞星火与Motionverse打造实时对话虚拟客服

Unity集成讯飞星火与Motionverse打造实时对话虚拟客服

1. 项目概述与核心价值最近在做一个虚拟展厅的项目,客户提了个挺有意思的需求:希望展厅里的虚拟客服不仅能回答预设问题,还能像真人一样,根据访客的实时提问,进行自然、流畅的对话,并且最好能配上相应的口型…

2026/11/16 21:12:24 阅读更多 →
基于LLM Agent的推荐系统智能诊断:从规则引擎到自主推理的实践

基于LLM Agent的推荐系统智能诊断:从规则引擎到自主推理的实践

1. 从“调接口”到“会思考”:推荐系统诊断的范式跃迁在推荐系统的日常迭代和维护中,我们常常陷入一种“消防员”式的被动响应模式。线上指标(如点击率、转化率)突然下跌,业务方一个电话过来,我们第一反应是…

2026/11/13 7:22:14 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/16 8:22:12 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/16 6:41:34 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/16 8:22:12 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/16 8:22:12 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/16 7:30:18 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/16 8:22:12 阅读更多 →

日新闻

周新闻

月新闻