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

// 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 Downloding
    const 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值