欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 培训 > 鸿蒙开发 -本地数据库操作

鸿蒙开发 -本地数据库操作

2024/10/25 11:24:59 来源:https://blog.csdn.net/qq_33067629/article/details/140413451  浏览:    关键词:鸿蒙开发 -本地数据库操作
// 1导入模块
import relationalStore from '@ohos.data.relationalStore';export class AthUserDbManager{//2.获取RdbStore实例,要注意的是,此处的getContext(this)用于获取应用上下文:getcreatDbtable(dbname:string){//配置数据库信息:const STORE_CONFIG :relationalStore.StoreConfig= {name: dbname, // 数据库文件名securityLevel: relationalStore.SecurityLevel.S1, // 数据库安全级别encrypt: false, // 可选参数,指定数据库是否加密,默认不加密// dataGroupId: 'dataGroupID' // 可选参数,仅可在Stage模型下使用,表示为应用组ID,需要向应用市场获取。指定在此Id对应的沙箱路径下创建实例,当此参数不填时,默认在本应用沙箱目录下创建。};//CCDownloding  CacheCourse  CacheSubCourse Downlodingconst SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS WORKER (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, GENDER TEXT NOT NULL)';relationalStore.getRdbStore(getContext(this),STORE_CONFIG,(err,store)=>{if (err) {console.error(`Failed to get RdbStore. Code:${err.code}, message:${err.message}`);return;}console.info('Succeeded in getting RdbStore.');// 当数据库创建时,数据库默认版本为0// if (store.version === 0) {store.executeSql(SQL_CREATE_TABLE); // 创建数据表// 设置数据库的版本,入参为大于0的整数//  store.version = 3;// }})}//3.插入数据insetDBData(dbname:string){const STORE_CONFIG :relationalStore.StoreConfig= {name: dbname, // 数据库文件名securityLevel: relationalStore.SecurityLevel.S1, // 数据库安全级别enc

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com