零基础学鸿蒙编程-轻量级数据库
2023-09-27 14:27:30 时间
什么是轻量级数据库
轻量级数据库是一种以键值对形式保存数据的存储方式.每条数据都需要指定一个唯一键名来进行区分.可以存储布尔型、整型、字符串等基础数据类型.其特点为简单、轻量,适合保存少量简单类型的数据,不适合保存大批量或复杂类型的数据.
基础样例
1. 写入和读取数据
- java代码
public class MainAbilitySlice extends AbilitySlice {
private Preferences preferences;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
String filename = "pdb";
preferences = databaseHelper.getPreferences(filename);
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());
}
private void write() {
preferences.putString("name", "花生皮编程");
preferences.flush();
}
private void read() {
String name = preferences.getString("name", "数据不存在");
new ToastDialog(getContext()).setText(name).show();
}
private void modify() {
preferences.putString("name", "花生皮编程2");
preferences.flush();
}
private void del() {
preferences.delete("name");
}
}
- 对应页面布局文件:
<?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>
常用函数说明
函数名 | 用途 |
---|---|
putString | 存储字符串类型数据 |
putInt | 存储整型数据 |
putLong | 存储长整型数据 |
putFloat | 存储浮点型数据 |
putBoolean | 存储布尔值,true或false |
putStringSet | 存储字符串集合 |
delete | 删除指定键名对应的数据记录 |
clear | 清空所有存储的数据 |
apply | 修改数据后,提交保存到文件中 |
getString | 以字符串类型读取出数据 |
getInt | 以整型读取出数据 |
getLong | 以长整型读取出数据 |
getFloat | 以浮点型读取出数据 |
getBoolean | 以布尔值读取出数据 |
完整源代码
https://gitee.com/hspbc/harmonyos_demos/tree/master/preferenceDemo
零基础系列
《零基础学安卓编程》
《零基础学Java编程》
《零基础学鸿蒙编程》
关于我
厦门大学计算机专业 | 前华为工程师
专注《零基础学编程系列》,包含:Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
全网可关注:花生皮编程
相关文章
- 数据库-MySQL基础(9)-多表关系
- 数据库-MySQL-基础(6)- DCL
- 数据库-MySQL-基础(4)-DQL(基础查询)
- 数据库-MySQL-基础(2)-DDL操作
- 数据库-MySQL-基础 (1)
- GaussDB(for MySQL)如何快速创建索引?华为云数据库资深架构师为您揭秘
- 13万张表+数亿行代码,迁移只需数小时,还是异构数据库
- Vc数据库编程基础MySql数据库的表增删改查数据
- oracle数据库转mysql数据要点
- Python连接SQL Server数据库 - pymssql使用基础
- [转载]数据库基础学习随记
- mysql基础:数据库的创建,增删改查
- 数据库事务系列-事务模型基础
- SQL Server2012数据库基础及应用
- MySQL数据库高并发优化配置
- MySQL<数据库入门>
- 数据库优化策略小结
- 阿里云内存数据库Memcache升级
- 数据库(2)数据库介绍
- 什么是ER图?数据库ER图基础概念整理
- 主流列式数据库评测:InfiniDB
- 如何使用数据库实现多台服务器竞争资源的问题
- 关于教育基础数据库的登录名生成问题
- Oracle 数据库优化-分析现有的sql
- 数据库事务学习笔记