做app网站的软件有哪些内容吗,cent os wordpress,做电商网站价格表,求网站晚上睡不着2021Harmony os Next——关系型数据库relationalStore.RdbStore的使用 描述数据库的使用建表定义表信息创建数据库表 创建数据库操作对象增更新查询删数据库的初始化 描述
本文通过存储一个简单的用户信息到数据库中为例#xff0c;进行阐述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) // 匹配表中主键为keyusername的字段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(): PromiseArrayUserProfileModel{try {let sql select * from UserProfileManagerUtil.USER_PROFILE_TABLE_NAMElet result await this.storeInstance?.querySql(sql)let array: ArrayUserProfileModel []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) // 匹配表中主键为keyusername的字段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) //创建数据库
}