HarmonyOS | 项目开发练习 「钢笔单词」 #5 关系型数据库工具类、一些模型类以及一些 JSON 文件 | 涉及弹性布局(FLEX)、用户首选项、关系型数据库

HarmonyOS | 项目开发练习 「钢笔单词」 #5 关系型数据库工具类、一些模型类以及一些 JSON 文件 | 涉及弹性布局(FLEX)、用户首选项、关系型数据库

项目结构:

在这里插入图片描述

模型:

Word.ets 单词

``

export default class Word {
  id?: number // 唯一标识
  word: string // 单词本身
  soundmark: string // 音标
  paraphrase: string // 单词释义
  audioLink: string = '' // 音频地址
  sentence: string // 例句

  constructor(word: string, soundmark: string, paraphrase: string, audioLink: string, sentence: string) {
    this.word = word;
    this.soundmark = soundmark;
    this.paraphrase = paraphrase;
    this.audioLink = audioLink;
    this.sentence = sentence;
  }
}

Book.ets 词书

``

export default class Book {
  bookName: string
  bookBackground: ResourceStr
  type: string
  inf: string
  publisher: string

  constructor(bookName: string, bookBackground: ResourceStr, type: string, inf: string, publisher: string) {
    this.bookName = bookName;
    this.bookBackground = bookBackground;
    this.type = type;
    this.inf = inf;
    this.publisher = publisher;
  }
}

一些 JSON 文件:

在这里插入图片描述

color.json 存放了一些颜色

``

{
  "color": [
    {
      "name": "start_window_background",
      "value": "#FFFFFF"
    },
    {
      "name": "welcome_page_background",
      "value": "#FFFFFFFF"
    },
    {
      "name": "index_page_background",
      "value": "#FFF6F6F6"
    },
    {
      "name": "primary_color",
      "value": "#ff281883"
    },
    {
      "name": "light_primary_color",
      "value": "#FF5A6086"
    },
    {
      "name": "lightest_primary_color",
      "value": "#FF8288B1"
    },
    {
      "name": "white",
      "value": "#FFFFFFFF"
    },
    {
      "name": "light_gray",
      "value": "#FFD2D2D2"
    },
    {
      "name": "gray",
      "value": "#FF737373"
    },
    {
      "name": "background_gray",
      "value": "#FFF5F5F5"
    }
  ]
}

string.json 存放了一些字符串

``

{
  "string": [
    {
      "name": "module_desc",
      "value": "module description"
    },
    {
      "name": "EntryAbility_desc",
      "value": "description"
    },
    {
      "name": "EntryAbility_label",
      "value": "label"
    },
    {
      "name": "user_privacy_title",
      "value": "Welcome to use Pen Word"
    },
    {
      "name": "user_privacy_content",
      "value": "Thanks for trusting and using Sonic Skyshatter! We value, protect, and defend privacy. We believe in transparency so that individuals and organizations can control their data and have clear choices about how it is used. We provide privacy choices for every user of our products and services and work to maintain them."
    },
    {
      "name": "agree_label",
      "value": "Agree"
    },
    {
      "name": "refuse_label",
      "value": "Refuse"
    },
    {
      "name": "tab_home_page",
      "value": "Main"
    },
    {
      "name": "tab_board",
      "value": "Board"
    },
    {
      "name": "tab_music_hall",
      "value": "Hall"
    },
    {
      "name": "tab_user",
      "value": "User"
    },
    {
      "name": "tab_recorder",
      "value": "Recorder"
    },
    {
      "name": "welcome_supports",
      "value": "Sonic Skyshatter supports"
    },
    {
      "name": "welcome_network",
      "value": " "
    },
    {
      "name": "confirm_label",
      "value": "Confirm"
    },
    {
      "name": "cancel_label",
      "value": "Cancel"
    },
    {
      "name": "search_text",
      "value": "Query here..."
    }
  ]
}

``

{
  "string": [
    {
      "name": "module_desc",
      "value": "主模块"
    },
    {
      "name": "EntryAbility_desc",
      "value": "钢笔单词应用程序"
    },
    {
      "name": "EntryAbility_label",
      "value": "钢笔单词"
    },
    {
      "name": "user_privacy_title",
      "value": "绝妙的开始!欢迎使用钢笔单词。"
    },
    {
      "name": "user_privacy_content",
      "value": "感激您信任并使用声破天!我们重视、保护和捍卫隐私。 我们信奉透明,以便人们和组织可以控制他们的数据,并在数据使用方式上具有明确的选择。 我们为使用我们产品和服务的每位用户提供隐私选择并加以维护。"
    },
    {
      "name": "agree_label",
      "value": "允许"
    },
    {
      "name": "refuse_label",
      "value": "拒绝"
    },
    {
      "name": "tab_home_page",
      "value": "主干"
    },
    {
      "name": "tab_board",
      "value": "布告板"
    },
    {
      "name": "tab_music_hall",
      "value": "厅"
    },
    {
      "name": "tab_user",
      "value": "我"
    },
    {
      "name": "tab_recorder",
      "value": "录音器"
    },
    {
      "name": "welcome_supports",
      "value": "声破天支持"
    },
    {
      "name": "welcome_network",
      "value": "网络"
    },
    {
      "name": "confirm_label",
      "value": "确定"
    },
    {
      "name": "cancel_label",
      "value": "取消"
    },
    {
      "name": "search_text",
      "value": "在这里进行查询..."
    }
  ]
}

在这里插入图片描述

数据库:

​ 实际上本项目没有实现持久化储存,以下代码仅供参考。

WordModel.ets 封装了数据库的初始化以及增删改查

``

import relationalStore from '@ohos.data.relationalStore'
import Word from '../model/Word'
class WordModel {
  tableName: string = 'custombook'
  private rdbStore: relationalStore.RdbStore

  initWordDB(context){
    // RDB 配置
    const config = {
      name: 'PenWord.db',
      securityLevel: relationalStore.SecurityLevel.S1
    }

    // 生词本的数据库表创建语句
    const CREATE_CUSTOM_TABLE_SQL = `CREATE TABLE IF NOT EXISTS custombook (
    word VARCHAR(64) PRIMARY KEY,
    soundmark VARCHAR(64),
    paraphrase VARCHAR(512),
    sentence VARCHAR(128),
    audioLink VARCHAR(256)
    )`

    relationalStore.getRdbStore(context, config, (err, rdbStore) => {
      if(err){
        console.error('获取 rdbStore 失败-' + err.message)
        return
      }
      rdbStore.executeSql(CREATE_CUSTOM_TABLE_SQL)
      console.log('创建生词本数据表成功—' + rdbStore)
      this.rdbStore = rdbStore
    })
  }

  async getWordList(){
    let result = await this.rdbStore.query(new relationalStore.RdbPredicates(this.tableName), ['word', 'soundmark', 'paraphrase', 'sentence', 'audioLink'])
    let words: Word[] = []

    while (!result.isAtLastRow){
      result.goToNextRow()

      let word = result.getString(result.getColumnIndex('word'))
      let soundmark = result.getString(result.getColumnIndex('soundmark'))
      let paraphrase = result.getString(result.getColumnIndex('paraphrase'))
      let sentence = result.getString(result.getColumnIndex('sentence'))
      let audioLink = result.getString(result.getColumnIndex('audioLink'))

      words.push(new Word(word, soundmark, paraphrase, sentence, audioLink))
    }
    console.log('数据获取成功-' + JSON.stringify(words))
    return words
  }

  addWord(word: string, soundmark: string, paraphrase: string, audioLink: string, sentence: string) : Promise<number> {
    return this.rdbStore.insert(this.tableName, {word, soundmark, paraphrase, sentence, audioLink})
  }

  updateWord(word: string, soundmark: string, paraphrase: string, audioLink: string, sentence: string){
    let newData = {word, soundmark, paraphrase, sentence, audioLink}
    let predicates = new relationalStore.RdbPredicates(this.tableName)
    predicates.equalTo('word', word)

    return this.rdbStore.update(newData, predicates)
  }

  deleteWord(word: string, soundmark: string, paraphrase: string, audioLink: string, sentence: string){
    let predicates = new relationalStore.RdbPredicates(this.tableName)
    predicates.equalTo('word', word)

    return this.rdbStore.delete(predicates)
  }
}

let wordModel = new WordModel()

export default wordModel as WordModel

EntryAbility.ets 初始化生词本数据表

``

...
async onCreate(want, launchParam) {
  await PreferenceUtil.loadPreference(this.context); // 加载用户首选项

  WordModel.initWordDB(this.context) // 初始化生词本数据表

  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
}
...

本项目到此为止。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JayHsu_蔚蓝审敛法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值