三种形式 首选项 键值型数据库 关系型数据库
首选项Preferences
- 用户首选项(Preferences):应用存入数据到Preferences实例后,可以使用flush()方法实现数据持久化。适用于比较小的数据存储 例如个人信息 个人特殊设置
- 1 创建首选项实例
- 2 然后可以创建数据 删除 增加 修改 是否存在等
键值型KV-Store
创建KVManager创建KVManager对象实例 然后getKVStore
然后put get delete
import { distributedKVStore } from '@kit.ArkData'
import { common } from '@kit.AbilityKit'
@Entry
@Component
struct KeyValue {
private db: distributedKVStore.SingleKVStore | null = null
async aboutToAppear() {
let ctx = getContext() //是个函数
const kvManagerConfig: distributedKVStore.KVManagerConfig = {
context: ctx, bundleName: "com.example.keyvaluestore"
}
let manager = distributedKVStore.createKVManager(kvManagerConfig)
this.db = await manager.getKVStore("mystore1", { securityLevel: distributedKVStore.SecurityLevel.S1 })//第一个是表的名字 第二个是一些参数
}
build() {
Column() {
Button("存储").onClick(() => {
this.db?.put("name", "小明")
console.log("存储成功")
})
Button("获取").onClick(async () => {
let v = await this.db?.get("name")
console.log(v as string)
})
}
}
}
使用安全级别 S1 S2 S3 S4 S4级别最高 常用银行宗教政治方面
关系型数据库 sqlite羽量级
1 获得一个getRdbStore()获得一个RdbStore,
2使用SQL语句进行操作数据库
注意对数据库进行改变之后要要更新版本