当前位置: 首页 > news >正文

起飞页做网站手机端关键词排名优化软件

起飞页做网站,手机端关键词排名优化软件,css做网站怎么添加子页,软件开发公司简介怎么写Harmony os Next——关系型数据库relationalStore.RdbStore的使用 描述数据库的使用建表定义表信息创建数据库表 创建数据库操作对象增更新查询删数据库的初始化 描述 本文通过存储一个简单的用户信息到数据库中为例,进行阐述relationalStore.RdbStore数据库的CRUD…

Harmony os Next——关系型数据库relationalStore.RdbStore的使用

  • 描述
  • 数据库的使用
    • 建表
      • 定义表信息
      • 创建数据库表
    • 创建数据库操作对象
    • 更新
    • 查询
    • 数据库的初始化

描述

本文通过存储一个简单的用户信息到数据库中为例,进行阐述relationalStore.RdbStore数据库的CRUD相关操作

数据库的使用

若是使用频繁,可以创建一个单例类进行管理存储到数据库的用户信息

export class UserProfileManagerUtil{private constructor() {}private static instance: UserProfileManagerUtil | null = nullpublic static getInstance(): UserProfileManagerUtil{if (UserProfileManagerUtil.instance == null) {UserProfileManagerUtil.instance = new UserProfileManagerUtil()}return UserProfileManagerUtil.instance}}

建表

定义表信息

通过SQL语句建立一个数据库表。在此之前定义数据库名称和用户信息表名以及用户信息表中的字段,此处为了简介,便只定义三个字断

  private static readonly DATABASE_NAME = "AppDataBase.db" //数据库名称private static readonly USER_PROFILE_TABLE_NAME = "UserProfile" //表名private static readonly COL_USERNAME = "USERNAME" private static readonly COL_AGE = "AGE" private static readonly COL_SEX = "SEX" 

配置数据库信息以及是否加密等

   private static readonly STORE_CONFIG :relationalStore.StoreConfig= {name: UserProfileManagerUtil.DATABASE_NAME, // 数据库文件名securityLevel: relationalStore.SecurityLevel.S1, // 数据库安全级别encrypt: true // 可选参数,指定数据库是否加密,默认不加密}

创建数据库表

下列SQL语句格式需要特别注意,不然容易在创建表的时候出错,下列以USERNAME为主键

  private static readonly CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + UserProfileManagerUtil.USER_PROFILE_TABLE_NAME + " ( " +UserProfileManagerUtil.COL_USERNAME + " TEXT PRIMARY KEY, " +UserProfileManagerUtil.COL_AGE + " INTEGER, " +UserProfileManagerUtil.COL_SEX + " INTEGER" +")"

创建数据库操作对象

根据上面的数据库配置信息和SQL建表语句,创建数据库操作对象relationalStore.getRdbStore
同时需要注意的是应用创建的数据库与其上下文(Context)有关,即使使用同样的数据库名称,但不同的应用上下文,会产生多个数据库,例如每个UIAbility都有各自的上下文。所以一般都在对应的UIAbility下先进行创建。确保在其UIAbility的可行性和唯一性

 // 应用创建的数据库与其上下文(Context)有关,即使使用同样的数据库名称,但不同的应用上下文,会产生多个数据库,例如每个UIAbility都有各自的上下文。async createDataBase(context: Context) {try {let store = await relationalStore.getRdbStore(context, UserProfileManagerUtil.STORE_CONFIG)if (store){this.storeInstance = storeawait this.storeInstance.executeSql(UserProfileManagerUtil.CREATE_TABLE) //创建数据库}} catch (error) {Logger.error(`the result is failed of create DB,the cause is ${(error as BusinessError).message}`)}}

根据上述创建的数据库操作对象进行插入操作,其中ValuesBucket使用键值对的方式进行数据匹对

  insertUserProfile(userProfile: UserProfileModel){let insertFormat: ValuesBucket = {'USERNAME' : userProfile.id,'AGE' : userProfile.avatar,'SEX' : userProfile.name}try {this.storeInstance?.insert(UserProfileManagerUtil.USER_PROFILE_TABLE_NAME, insertFormat, (err, rowId)=>{if (err) {Logger.error(`insert failed,the cause is ${err.message}`)return}//插入成功Logger.info(`insert successful,the rowId is ${rowId}`)})} catch (error) {Logger.error(`insert failed,the cause is ${(error as BusinessError).message}`)}}

更新

下列通过predicates.equalTo查找数据库表中对应的数据项,然后根据ValuesBucket中的内容定位到需要进行数据更新的字段,下列以更新年龄为例子。

  updateAge(primaryKey: string, age: number){try {let predicates = new relationalStore.RdbPredicates(UserProfileManagerUtil.USER_PROFILE_TABLE_NAME) // 创建表的predicates-谓词predicates.equalTo(UserProfileManagerUtil.COL_USERNAME, primaryKey) // 匹配表中主键为key(username)的字段let updateFormat: ValuesBucket = {'AGE' : age}this.storeInstance?.update(updateFormat, predicates, (err: BusinessError, rows: number) => {if (err) {Logger.error(`update failed,the cause is ${err.message}`)return}//更新数据成功Logger.info(`update successful,the rowId is ${rows}`)})} catch (error) {Logger.error(`update failed,the cause is ${(error as BusinessError).message}`)}}

查询

定义数据库查询SQL语句

let sql = "select * from " + UserProfileManagerUtil.USER_PROFILE_TABLE_NAME

通过querySql查询数据库表中的内容,然后通过遍历查询结果,并取出其中的内容。其中getString(0)后面的序号,为创建数据库表时,字段的定义顺序

  async queryUserProfile(): Promise<Array<UserProfileModel>>{try {let sql = "select * from " + UserProfileManagerUtil.USER_PROFILE_TABLE_NAMElet result = await this.storeInstance?.querySql(sql)let array: Array<UserProfileModel> = []if(result) {while(result.goToNextRow()) {let username = result.getString(0)let age = result.getLong(1)let sex = result.getLong(2)array.push(new UserProfileModel(username,age,sex));}result.close()}return array} catch (error) {Logger.error(`query failed,the cause is ${(error as BusinessError).message}`)return null}}

通过predicates.equalTo比对数据库表中数据项的主键,然后删除对应列

  deleteUserProfile(primaryKey: string){try {let predicates = new relationalStore.RdbPredicates(UserProfileManagerUtil.USER_PROFILE_TABLE_NAME) // 创建表的predicates-谓词predicates.equalTo(UserProfileManagerUtil.COL_USERNAME, primaryKey) // 匹配表中主键为key(username)的字段this.storeInstance?.delete(predicates, (err: BusinessError, rows: number) => {if (err) {Logger.error(`delete failed,the cause is ${err.message}`)return}//删除成功Logger.info(`delete successful,the rowId is ${rows}`)})} catch (error) {Logger.error(`delete failed,the cause is ${(error as BusinessError).message}`)}}

数据库的初始化

可以在UIAbility的派生类中进行数据库创建,确保到当前Context的唯一性和可行性。避免在未初始化就调用,UIAbility因此在入口处进行调用。

export default class EntryAbility extends UIAbility {async onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {await UserProfileManagerUtil.getInstance().createDataBase(this.context) //创建数据库
}
http://www.laogonggong.com/news/30849.html

相关文章:

  • 帮卖驾驶证的做网站东莞做一个企业网站
  • 旅游网站建设平台分析站长推荐黄色
  • 网站规划网站建设报价表windows 优化大师
  • 网站可以更换域名吗seo技术快速网站排名
  • wordpress开启子域名多站点模式如何做网络营销
  • wordpress文章采集发布插件厦门seo排名
  • 网站首页设计排版要点网络推广方案范例
  • 京东联盟网站推广位怎么做刚出来的新产品怎么推
  • 凡客建站登录搜索引擎优化的含义和目标
  • 网络科技公司网站制作网盘手机app官网下载
  • 电子商务网站建设的主页域名注册查询工具
  • 搭建个网站需要多少钱百度百家号注册
  • 做招投标网站专业营销团队公司
  • 政府 网站管理系统 网络 架构搜索引擎 磁力吧
  • 设计师接单平台网站下拉关键词排名
  • 政府网站发展趋势及建设思路现代营销手段有哪些
  • 虚拟主机怎么搭建网站关系网站优化公司
  • 做宣传片的网站网络舆情应急预案
  • 建网站和开发软件哪个难关键词英文
  • ui中国常州百度关键词优化
  • 厦门做网站公司有哪些31省市新增疫情最新消息
  • 2021室内设计公司排名重庆高端网站seo
  • 网站响应式和非响应式专业做网络推广的公司
  • 做网站点击量有用吗郑州网站建设推广
  • 深圳网站建设 易通鼎培训机构排名前十
  • 福田做网站公司在百度怎么发广告做宣传
  • 网站建设工程师 html5域名ip查询入口
  • 用asp.net做简易网站天津seo优化公司
  • 网站网络优化最近的新闻大事10条
  • 做问卷调查赚钱网站好seo搜索