Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
J
jump_hm_warehouse
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Members
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
毛勇泽
jump_hm_warehouse
Commits
21554b3f
Commit
21554b3f
authored
Jan 29, 2024
by
ning
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
数据库升级与创建操作
parent
7fa73a87
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
121 行增加
和
0 行删除
+121
-0
AbstractDbOption.ets
common/src/main/ets/db/util/AbstractDbOption.ets
+18
-0
DbHelper.ets
common/src/main/ets/db/util/DbHelper.ets
+103
-0
没有找到文件。
common/src/main/ets/db/util/AbstractDbOption.ets
0 → 100644
View file @
21554b3f
import relationalStore from '@ohos.data.relationalStore';
/**
* 数据库操作
*/
export abstract class AbstractDbOption {
/**
* 数据库对象-创建
*/
abstract onCreate(db: relationalStore.RdbStore)
/**
* 数据库对象-版本
*/
abstract onUpgrade(db: relationalStore.RdbStore, oldVersion: number, newVersion: number)
}
\ No newline at end of file
common/src/main/ets/db/util/DbHelper.ets
0 → 100644
View file @
21554b3f
import relationalStore from '@ohos.data.relationalStore';
import dataPreferences from '@ohos.data.preferences'
import { AbstractDbOption } from './AbstractDbOption';
import { Logger } from '../../utils/Logger';
/**
* 数据工具类
*/
class DbHelper {
dbContext: Context;
dbName: string = '';
dbVersion: number = 0;
rdbStore: relationalStore.RdbStore;
/**
* 初始化数据库
* @param context
* @param dbName
* @param dbVersion
* @param dbOpts
*/
async initDb(context: Context, dbName: string, dbVersion: number, dbOpts: AbstractDbOption) {
if (dbVersion <= 0) {
throw new Error("数据库版本必须大于0");
}
this.dbContext = context;
this.dbName = dbName;
this.dbVersion = dbVersion;
if (this.rdbStore) {
this.rdbStore = null;
}
//定义Preference缓存键
let dbPreferenceKey = `junmp_db_preference`;
let dbVersionKey = `junmp_db_version_${dbName}`;
//去当前的缓存
let preferences = await dataPreferences.getPreferences(context, dbPreferenceKey);
let oldVersion = (await preferences.get(dbVersionKey, 0)) as number
if (this.dbVersion < oldVersion) {
this.dbVersion = oldVersion;
}
//获取RDB对象
await this.getRdbStore();
if (oldVersion != this.dbVersion) {
await dbOpts.onCreate(this.rdbStore);
if (oldVersion < this.dbVersion) {
this.beginTransaction();
await dbOpts.onUpgrade(this.rdbStore, oldVersion, this.dbVersion);
this.commit();
}
//把新的版本信息刷进缓存
await preferences.put(dbVersionKey, this.dbVersion);
await preferences.flush();
}
}
/**
* 获取RDB对象
*/
getRdbStore(): Promise<relationalStore.RdbStore> {
return new Promise((resolve, reject) => {
if (this.rdbStore) {
resolve(this.rdbStore);
} else {
relationalStore.getRdbStore(this.dbContext, {
name: this.dbName,
securityLevel: relationalStore.SecurityLevel.S1
}).then((store) => {
this.rdbStore = store;
resolve(store)
}).catch(e => {
Logger.error(this, '初始化RDB异常', e);
reject(e)
})
}
})
}
beginTransaction() {
if (this.rdbStore) {
Logger.info(this, "事务开始")
this.rdbStore.beginTransaction()
}
}
commit() {
if (this.rdbStore) {
Logger.info(this, "事务提交")
this.rdbStore.commit()
}
}
rollBack() {
if (this.rdbStore) {
Logger.info(this, "事务回滚")
this.rdbStore.rollBack()
}
}
}
export default new DbHelper();
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论