HarmonyOS 鸿蒙Next RDB查询表返回-1
HarmonyOS 鸿蒙Next RDB查询表返回-1
有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html
关于HarmonyOS 鸿蒙Next RDB查询表返回-1的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。
执行query,返回-1。
我创建了一个类用于管理SQL:
import relationalStore from '@ohos.data.relationalStore';
import hilog from '@ohos.hilog';
import {VideoModel,UpModel} from '../model/request_model'
import {get_up_info} from '../network/http_request'
const VIDEOS_COLUMNS=['up_uid','up_name','view','comment','like','dynamic_id','timestamp','bvid','title','description','duration','jump_url','pic','tname','usr_action_txt','user_read','sys_update_time']
const UPS_COLUMNS=['uid','uname','face','group','starred']
const CREATE_TABLE_UPS_SQL=CREATE TABLE IF NOT EXISTS ups( uid INTEGER PRIMARY KEY, uname TEXT, face TEXT, up_group TEXT, starred BOOLEAN )
export class SqlManagerUp{
store:relationalStore.RdbStore
//获取数据库管理对象
async getStoreInstance(){
if(this.store){
this.store.executeSql(CREATE_TABLE_UPS_SQL)
return this.store}
else{
this.store=await relationalStore.getRdbStore(getContext(),{
name:‘data.db’,
securityLevel:relationalStore.SecurityLevel.S1
},)
await this.store.executeSql(CREATE_TABLE_UPS_SQL)
this.store.commit()
const predicates = new relationalStore.RdbPredicates(‘ups’)
this.store.query(predicates).then((resultSet)=>{hilog.debug(0x8888,‘my_test_data_sql’,‘在getStore中被查到长度是’+resultSet.rowCount)})
hilog.debug(0x8888,‘my_test_data_sql’,‘未发现数据库,自动创建’)
return this.store
}
}
//关注UP
async followUp(up_uid:number){
const store = await this.getStoreInstance()
const predicates = new relationalStore.RdbPredicates(‘ups’)
predicates.equalTo(‘up_uid’,up_uid)
const resultSet = await store.query(predicates)
const up_info = get_up_info(up_uid)
if(resultSet.rowCount==0){
const values={
up_uid:up_uid,
up_name:(await up_info)[‘uname’],
face:(await up_info)[‘face’],
up_group:’’,
starred:false
}
const insertResult = await store.insert(‘ups’,values)
hilog.debug(0x8888,‘my_test_data_sql’,‘关注UP成功,’+up_uid+’,status=’+insertResult)
return insertResult
}else{
hilog.debug(0x8888,‘my_test_data_sql’,‘UP已关注,不可以重复关注:’+up_uid+‘rowCount=’+resultSet.rowCount)
return -1
}
}
//取关UP
async unfollowUp(up_uid:number){
const store = await this.getStoreInstance()
const predicates = new relationalStore.RdbPredicates(‘ups’)
predicates.equalTo(‘up_uid’,up_uid)
const deleteResult = await store.delete(predicates)
hilog.debug(0x8888,‘my_test_data_sql’,‘取关UP成功,’+up_uid+’,status=’+deleteResult)
return deleteResult
}
//移入特别关注
async starUp(up_uid:number){
const store = await this.getStoreInstance()
const predicates = new relationalStore.RdbPredicates(‘ups’)
predicates.equalTo(‘up_uid’,up_uid)
const values={
starred:true
}
const updateResult = await store.update(values,predicates)
hilog.debug(0x8888,‘my_test_data_sql’,‘移入特别关注成功,’+up_uid+’,status=’+updateResult)
return updateResult
//TODO:连接UI
}
//移出特别关注
async unstarUp(up_uid:number){
const store = await this.getStoreInstance()
const predicates = new relationalStore.RdbPredicates(‘ups’)
predicates.equalTo(‘up_uid’,up_uid)
const values={
starred:false
}
const updateResult = await store.update(values,predicates)
hilog.debug(0x8888,‘my_test_data_sql’,‘移出特别关注成功,’+up_uid+’,status=’+updateResult)
return updateResult
}
//改变或删除分组
async changeGroup(up_uid:number,up_group:string){
const store = await this.getStoreInstance()
const predicates = new relationalStore.RdbPredicates(‘ups’)
predicates.equalTo(‘up_uid’,up_uid)
const values={
up_group:up_group
}
const updateResult = await store.update(values,predicates)
hilog.debug(0x8888,‘my_test_data_sql’,‘改变分组成功,’+up_uid+’,status=’+updateResult+’,group=’+up_group)
return updateResult
}
async getUpInfo(up_uid:number){
const store = await this.getStoreInstance()
const predicates = new relationalStore.RdbPredicates(‘ups’)
predicates.equalTo(‘up_uid’,up_uid)
const resultSet=await store.query(predicates)
if(resultSet.rowCount==0){
return get_up_info(up_uid)
}
else{
let model=new UpModel(
resultSet.getLong(0),
resultSet.getString(1),
resultSet.getString(2),
resultSet.getString(3),
Boolean(resultSet.getLong(4))
)
hilog.debug(0x8888,‘my_test_data_sql’,‘获取UP信息成功,’+JSON.stringify(model))
return model
}}
async getUpsInfo(up_uids:number[]){
return up_uids.map(umgr.getUpInfo)
}
//获取关注的up的uid列表
async getFollowedUps(){
const store = await this.getStoreInstance()
const predicates = new relationalStore.RdbPredicates(‘ups’)
const resultSet = await store.query(predicates,UPS_COLUMNS)
const uids:Array<number>=[]
while(resultSet.goToNextRow()){
uids.push(resultSet.getLong(0))
}
hilog.debug(0x8888,‘my_test_data_sql’,‘获取UP列表成功,’+JSON.stringify(uids))
return Promise.all( uids.map(uid=>umgr.getUpInfo(uid)))
}
}
export const vmgr=new SqlManagerVideo()
vmgr.getStoreInstance()
export const umgr=new SqlManagerUp()
umgr.getStoreInstance()
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
其中UpModel类写在另一个文件里:
export class UpModel{
uid:number//UP主编号
uname:string//UP主名
face:string
group:string=’’//UP主分组
starred:boolean=false//是否已关注,默认为false
constructor(uid:number,uname:string,face:string,group:string=’’,starred:boolean=false) {
this.uid=uid;
this.uname=uname;
this.face=face;
this.group=group;
this.starred=starred;
return this
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
管理页面的UI文件:
export struct ups_tab{
@State followedUps:UpModel[]=[]
build(){
Stack({alignContent:Alignment.BottomEnd}){
List(){
ListItem(){Text(‘UP主个数’+this.followedUps.length).fontColor($r(‘app.color.text_color’))}
ForEach(this.followedUps,(up_uid:UpModel)=>{
ListItem(){Row(){
Text(up_uid.uname).width(‘100%’).height(‘100%’).fontSize(‘30px’).fontColor($r(‘app.color.text_color’)).textAlign(TextAlign.Start).padding({left:‘20px’,top:‘10px’,bottom:‘10px’})
Image(up_uid.face).width(‘50px’).height(‘50px’).borderRadius(90)
}}
},(up)=>up.uid)
}.width(‘95%’).height(‘100%’)
Column(){
Button(){Image($r(‘app.media.add’)).onClick(()=>router.pushUrl({url:‘pages/add_up’}))
.fillColor(‘0xffffff’)}.borderRadius(90).width($r(‘app.integer.float_button_size’)).height($r(‘app.integer.float_button_size’)).padding(10).margin({right:10,bottom:10})
Button(){
Image($r(‘app.media.refresh’))
.fillColor(‘0xffffff’)}.onClick(()=>umgr.getFollowedUps().then((models)=>this.followedUps=models)).borderRadius(90).width($r(‘app.integer.float_button_size’)).height($r(‘app.integer.float_button_size’)).padding(10).margin({right:10,bottom:10})
}}}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
当开始时,UP主个数是0,这是应该的。我点击最后几行的那个刷新按钮,跳到getFollowedUps(在SQL Manager里),添加断点看到
resultSet.rowCount=-1
很奇怪啊!一开始表里有一个叫group的字段,发现这是坑之后改叫up_group了,没问题了。每次调用umgr里的项目也都会检查有没有存在this.store,保证都有。也没看到需要权限。另外添加up主,调用的
umgr.followUp(传入UID)
调用结束后马上看getFollowedUps也是-1
这是为什么?我明明执行创建的命令了。
另外,刚刚开始运行时会报日志“未发现数据库,自动创建”和“在getStore中被查到长度是0”,顺序反了而且这不是-1了
希望HarmonyOS能继续优化系统稳定性,减少崩溃和重启的情况。