HarmonyOS 数据库拷贝后无法读取

将db文件拷贝到HarmonyOS数据库文件中,读取rowCount总是-1,帮忙看下这样操作是否正确?代码如下:

import fs from '@ohos.file.fs'; 
import PiccLog from '../PiccLog'; 
import { relationalStore } from '@kit.ArkData'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
import { FindCityModle } from '../../model/city/FindCityModle'; 
 
export class DbUtils{ 
 
 
  dbName = 'cityname.db' 
 
  KHDBNAME = "TB_REGION_COMCODE";//客户俱乐部表名 
  DSDBNAME = "TB_REGION_COMCODE_DS";//电商表名 
  // DSTHIRDTABLENAME = "TB_REGION_THIRD_COMCODE_DS";//电商表名 
  DSTHIRDTABLENAME = "TB_REGION_THIRD_COM";//电商表名 
 
  /** 
   * 拷贝数据库到沙箱目录 
   */ 
  copyDataToDatabase(){ 
    let resMgr = getContext().resourceManager; 
    try { 
      let myBuffer: ArrayBufferLike = resMgr.getRawFileContentSync(this.dbName).buffer; 
      let dbFilePath = getContext().databaseDir + '/' + this.dbName; 
 
      let file = fs.openSync(dbFilePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
      let writeLen = fs.writeSync(file.fd, myBuffer); 
      console.info("testTag-write data to file succeed and size is:" + writeLen); 
      fs.closeSync(file); 
    } catch (e) { 
      PiccLog.error('database copyDataToDatabase error : ' + JSON.stringify(e)); 
    } 
    PiccLog.error('database copyDataToDatabase success : '); 
  } 
 
  getRdbStore(getRDStore:(data: relationalStore.RdbStore)=>void){ 
    const STORE_CONFIG: relationalStore.StoreConfig = { 
      name: this.dbName, 
      securityLevel: relationalStore.SecurityLevel.S1 
    }; 
    relationalStore.getRdbStore(getContext(), STORE_CONFIG, (err: BusinessError, rdbStore: relationalStore.RdbStore) => { 
      if (err) { 
        PiccLog.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 
        return; 
      } 
      PiccLog.info('database Get RdbStore successfully.'); 
      getRDStore(rdbStore); 
    }) 
  } 
 
  queryData(rdbStore: relationalStore.RdbStore){ 
    let predicates = new relationalStore.RdbPredicates(this.DSTHIRDTABLENAME); 
    rdbStore.query(predicates).then((result)=>{ 
      let arrCity:FindCityModle[] = []; 
      let rowCount = result.rowCount; 
      PiccLog.info('database rowCount : ' + rowCount) 
      while(result.goToNextRow()){ 
        let findCityModel:FindCityModle = new FindCityModle(); 
        findCityModel.adcode = result.getString(1); 
        findCityModel.cityCode = result.getString(2); 
        findCityModel.comCode = result.getString(3); 
        findCityModel.comCodeds = result.getString(4); 
        PiccLog.info("database queryData : adcode " + findCityModel.adcode + "cityCode :" + findCityModel.cityCode); 
      } 
    }) 
 
 
  } 
 
} 
 
export default new DbUtils();
HarmonyOS
2024-11-01 10:42:30
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

ROWCOUNT -1可能是表不存在,请确认"TB_REGION_THIRD_COM"这个表是否存在,将rawfile下的文件拷贝到沙箱目录可以通过这种方式,参考demo:

saveFileToCache(file: resourceManager.RawFileDescriptor, dbName:string) { 
  // 创建缓存文件(当前是覆盖式创建) 
  let cFile = getContext(this).getApplicationContext().databaseDir + "/entry/rdb/" + dbName 
  let cacheFile = fs.openSync(cFile, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE) 
  // 读取缓冲区大小 
  let bufferSize = 30000 
  let buffer = new ArrayBuffer(bufferSize); //创建buffer缓冲区 
  // 要copy的文件的offset和length 
  let currentOffset = file.offset; 
  let lengthNeedToReed = file.length; 
  let readOption:ReadOptions = { 
    offset: currentOffset, //期望读取文件的位置。可选,默认从当前位置开始读 
    length: bufferSize //每次期望读取数据的长度。可选,默认缓冲区长度 
  } 
  // 后面len会一直减,直到没有 
  while(true) { 
    // 读取buffer容量的内容 
    let readLength = fs.readSync(file.fd, buffer, readOption); 
    console.info("123") 
    // 写入buffer容量的内容 
    fs.writeSync(cacheFile.fd, buffer, {length:readLength}) //写到cacheFile里 
    // 判断后续内容 修改读文件的参数 
    // buffer没读满代表文件读完了 
    if (readLength < bufferSize) { 
      break; 
    } 
    if(readOption.offset){ 
      readOption.offset += readLength; 
    } 
  } 
  console.log("Copy Success!!!") 
  fs.close(cacheFile); 
} 
 
aboutToAppear(){ 
  let dirPath = getContext(this).getApplicationContext().databaseDir + "/entry" 
  fs.mkdirSync(dirPath); 
  dirPath = dirPath + "/rdb" 
  fs.mkdirSync(dirPath); 
  let dbName:string = 'cityname.db' 
  try { 
    getContext(this).resourceManager.getRawFd(dbName, (error, value:resourceManager.RawFileDescriptor) => { 
      if (error != null) { 
        console.log(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`); 
      } else { 
        console.info(value.length.toString() + "TEST") 
        this.saveFileToCache(value, dbName) 
      } 
    }); 
  } catch (error) { 
    console.error(`callback getRawFd failed, error code: ${error.code}, message: ${error.message}.`) 
  }; 
 
}
分享
微博
QQ
微信
回复
2024-11-01 17:39:24
相关问题
关系数据库无法拷贝怎么回事?
2003浏览 • 1回复 待解决
如何读取本地/预制数据库
1078浏览 • 1回复 待解决
HarmonyOS 数据库框架
316浏览 • 1回复 待解决
HarmonyOS db文件无法读取数据
494浏览 • 1回复 待解决
HarmonyOS 数据库数据更新怎么监听?
103浏览 • 1回复 待解决
HarmonyOS 数据库dataORM插入数据失败
156浏览 • 1回复 待解决
HarmonyOS 关系型数据库批量插入数据
239浏览 • 1回复 待解决
HarmonyOS 数据库多线程操作
118浏览 • 1回复 待解决
HarmonyOS relationalStore数据库设置监听
342浏览 • 1回复 待解决
HarmonyOS数据库数据变化是否可以监听
436浏览 • 1回复 待解决