#HarmonyOS NEXT 体验官#本地数据库实现《菜谱大全》app(三) 原创

一路向北545
发布于 2024-8-18 22:11
浏览
0收藏


 菜谱大全的UI框架也已搭建完毕,接下来就是通过本地数据库的操作来获取相应的数据支持了。

1.首页列表获取(带分页)

#HarmonyOS NEXT 体验官#本地数据库实现《菜谱大全》app(三)-鸿蒙开发者社区cke_245.png

//food_meau :表的名称
/**
 *
 * @param store 数据库实例
 * @param onGetFoods 获取数据成功后的回调,返回菜谱数组
 * @param onError
 * @param limit 每次获取数量限制
 * @param skip 
 */
getFoods(store: relationalStore.RdbStore, onGetFoods: (res: Array<Food>) => void, onError?: (err: string) => void,
  limit?: number, skip?: number) {
  // let predicates = new relationalStore.RdbPredicates("food_meau")
  let lim = ` limit 10`
  if (limit && skip) {
    lim = `limit ${limit} skip ${skip}`
  }


  let sql = `Select * from food_meau order By RANDOM() ${lim}`
  store.querySql(sql, (err: BusinessError, resultSet) => {
    if (err == undefined) {
      let foods = Array<Food>()
      while (resultSet.goToNextRow()) {
        foods.push(this.parseFood(resultSet))
      }
      onGetFoods(foods)
    } else

#HarmonyOS NEXT 体验官#本地数据库实现《菜谱大全》app(三)-鸿蒙开发者社区

菜谱数据的实体类

/**
 * 菜谱数据实体类
 */
export class Food {
  meauID: number = 0
  name: string = ''
  tags: string = ''
  method: string = ''
  img: string = ''
  url: string = ''
  level: string = ''
  peopleNum: string = ''
  taste: string = ''
  prepareTime: string = ''
  cookTime: string = ''
  mainIngredient: string = '' //主料
  accessories: string = '' //辅料
  measure: string = '' //步骤
  techniques: string = '' //技巧
  scrapyTime: string = ''

#HarmonyOS NEXT 体验官#本地数据库实现《菜谱大全》app(三)-鸿蒙开发者社区

对relationalStore.ResultSet返回数据进行解析

parseFood(resultSet: relationalStore.ResultSet): Food {
  let food = new Food()
  food.meauID = this.parseLong("meauID", resultSet)
  food.name = this.parseString("name", resultSet);
  food.tags = this.parseString("tags", resultSet);
  food.method = this.parseString("method", resultSet);
  food.img = this.parseString("img", resultSet);
  food.url = this.parseString("url", resultSet);
  food.level = this.parseString("level", resultSet);
  food.peopleNum = this.parseString("peopleNum", resultSet);
  food.taste = this.parseString("taste", resultSet);
  food.prepareTime = this.parseString("prepareTime", resultSet);
  food.cookTime = this.parseString("cookTime", resultSet);
  food.mainIngredient = this.parseString("mainIngredient", resultSet); //主料
  food.accessories = this.parseString("accessories", resultSet); //辅料
  food.measure = this.parseString("measure", resultSet); //步骤
  food.techniques = this.parseString("techniques", resultSet); //技巧
  food.scrapyTime = this.parseString("scrapyTime", resultSet);
  return food
}
parseLong(column: string, resultSet: relationalStore.ResultSet): number {
  if (resultSet.getColumnIndex(column) != -1) {
    return resultSet.getLong(resultSet.getColumnIndex(column));
  }
  return 0
}

parseString(column: string, resultSet: relationalStore.ResultSet): string {
  if (resultSet.getColumnIndex(column) != -1) {
    return resultSet.getString(resultSet.getColumnIndex(column));
  }
  return ""

#HarmonyOS NEXT 体验官#本地数据库实现《菜谱大全》app(三)-鸿蒙开发者社区

2.通过关键字来进行模糊搜索

/**
 *
 * @param store
 * @param keyword 关键字
 * @param onGetFoods
 * @param onError
 * @param limit
 * @param skip
 */
getFoodsByKeyword(store: relationalStore.RdbStore, keyword: string, onGetFoods: (res: Array<Food>) => void,
  onError?: (err: string) => void, limit?: number, skip?: number) {
  let lim = ` limit 10`//限制每次取10条数据
  if (limit && skip) {
    lim = `limit ${limit} skip ${skip}`
  }
  let sql = `Select * from food_meau where name like ("%${keyword}%") order By RANDOM() ${lim}`
  store.querySql(sql, (err: BusinessError, resultSet) => {
    if (err == undefined) {
      let foods = Array<Food>()
      while (resultSet.goToNextRow()) {
        //解析数据
        foods.push(this.parseFood(resultSet))
      }
      onGetFoods(foods)
    } else

#HarmonyOS NEXT 体验官#本地数据库实现《菜谱大全》app(三)-鸿蒙开发者社区

3.获取指定烹饪方法下的菜谱列表

#HarmonyOS NEXT 体验官#本地数据库实现《菜谱大全》app(三)-鸿蒙开发者社区cke_10485.png

/**
 * 获取指定烹饪方法下的菜谱列表
 * @param store
 * @param method
 * @param onGetFoods
 * @param onError
 * @param limit
 * @param skip
 */
getFoodsByMethod(store: relationalStore.RdbStore, method: string, onGetFoods: (res: Array<Food>) => void,
  onError?: (err: string) => void, limit?: number, skip?: number) {
  // let predicates = new relationalStore.RdbPredicates("food_meau")
  // store.query(predicates, (err: BusinessError, resultSet) => {
  let lim = ` limit 10`
  if (limit && skip) {
    lim = `limit ${limit} skip ${skip}`
  }

  let sql = `Select * from food_meau where method like ("%${method}%") ${lim}`
  store.querySql(sql, (err: BusinessError, resultSet) => {
    if (err == undefined) {
      let foods = Array<Food>()
      while (resultSet.goToNextRow()) {
        foods.push(this.parseFood(resultSet))
      }
      onGetFoods(foods)
    } else

#HarmonyOS NEXT 体验官#本地数据库实现《菜谱大全》app(三)-鸿蒙开发者社区

/**
 * 获取所有烹饪方法
 * @param store
 * @param onGetMethods
 * @param onError
 */
getAllCookMethods(store: relationalStore.RdbStore, onGetMethods: (res: Array<string>) => void,
  onError?: (err: string) => void) {
  let sql = "Select * from food_meau group by method ORDER BY method DESC"
  store.querySql(sql, (err: BusinessError, resultSet) => {
    if (err == undefined) {
      let arr: Array<string> = new Array();
      while (resultSet.goToNextRow()) {
        let food = this.parseFood(resultSet)
        arr.push(food.method)
      }
      onGetMethods(arr)
    } else

#HarmonyOS NEXT 体验官#本地数据库实现《菜谱大全》app(三)-鸿蒙开发者社区

4.获取所有口味

#HarmonyOS NEXT 体验官#本地数据库实现《菜谱大全》app(三)-鸿蒙开发者社区cke_19433.png

/**
 * 获取所有口味
 * @param store
 * @param onGetTaste
 * @param onError
 */
getAllTastes(store: relationalStore.RdbStore, onGetTaste: (res: Array<string>) => void,
  onError?: (err: string) => void) {
  let sql = "Select * from food_meau group by taste order by taste desc"
  store.querySql(sql, (err: BusinessError, resultSet) => {
    if (err == undefined) {
      let arr: Array<string> = new Array();
      while (resultSet.goToNextRow()) {
        let food = this.parseFood(resultSet)
        arr.push(food.taste)
      }
      onGetTaste(arr)
    } else

#HarmonyOS NEXT 体验官#本地数据库实现《菜谱大全》app(三)-鸿蒙开发者社区

5.根据难度获取菜谱列表

#HarmonyOS NEXT 体验官#本地数据库实现《菜谱大全》app(三)-鸿蒙开发者社区cke_30412.png

/**
 * 根据难度获取菜谱列表
 * @param store
 * @param level
 * @param onGetFoods
 * @param onError
 * @param limit
 * @param skip
 */
getFoodsByLevel(store: relationalStore.RdbStore, level: string, onGetFoods: (res: Array<Food>) => void,
  onError?: (err: string) => void, limit?: number, skip?: number) {
  let lim = ` limit 10`
  if (limit && skip) {
    lim = `limit ${limit} skip ${skip}`
  }

  let sql = `Select * from food_meau where level like ("%${level}%") ${lim}`
  store.querySql(sql, (err: BusinessError, resultSet) => {
    if (err == undefined) {
      let foods = Array<Food>()
      while (resultSet.goToNextRow()) {
        foods.push(this.parseFood(resultSet))
      }
      onGetFoods(foods)
    } else

#HarmonyOS NEXT 体验官#本地数据库实现《菜谱大全》app(三)-鸿蒙开发者社区


©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
标签
收藏
回复
举报
回复
    相关推荐