Deprecated: Function get_magic_quotes_gpc() is deprecated in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 99

Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 619

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1169

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176
8000 GitHub - ruirui1128/rdb
Nothing Special   »   [go: up one dir, main page]

Skip to content

ruirui1128/rdb

Repository files navigation

RDB

简介

在鸿蒙持久化数据sqlite的基础上封装,让开发者以对象的形式操作数据库表

下载安装

ohpm install @rui/rdb

使用

一 建立表结构

1 新建表对象继承BaseTable

2 tableName注解在类上标记表名称

3 columnType注解在类成员上标记表字段类型

@tableName("t_person")
export class Person extends BaseTable {
  @columnType(ColumnType.KEY_AUTO)  // 主键自增 必须
  id?: number;
  
  @columnType(ColumnType.TEXT)  // string
  name?: string;
  
  @columnType(ColumnType.INT) // 整型
  age?: number;
  
  @columnType(ColumnType.REAL) // 浮点
  score?: number
  
  @columnType(ColumnType.BOOLEAN) // 布尔
  isMan?: boolean

  // 构造方法必须 全部默认值
  constructor(name: string = '', age: number = 0,  score: number = 0.0, isMan: boolean = false ) {
    super()
    this.id = 0
    this.name = name;
    this.age = age;
    this.score = score
    this.isMan = isMan
  }
  
  // 非必须
  toString(): string {
    return `Person { id: ${this.id}, name: ${this.name}, age: ${this.age},
    score: ${this.score}, isMan: ${this.isMan}
     `;
  }

二 初始化数据库

EntryAbilityonCreate 方法中调用 Rdb.init 方法进行初始化


  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
    // oh系统默认StoreConfig
    const config: relationalStore.StoreConfig = {
      name: "my_db",
      securityLevel: relationalStore.SecurityLevel.S1,
    };
    
    const rdbConfig = new RdbConfig()
    rdbConfig.version = 1               // 数据库版本号
    rdbConfig.config = config           // 默认StoreConfig
    rdbConfig.tables = [Person]         // 数据库表
    Rdb.init(this.context, rdbConfig)   // 初始化
  }

三 增查改删

1 增

async function insertPersons() {

  // 单个新增
  const p = new Person()
  p.name = "张三丰"
  p.age = 150
  p.score = 99.9
  p.isMan = true
  await p.save() 


  // 批量新增
  const list: Person[] = []
  const p1 = new Person()
  p1.name = "张翠山"
  p1.age = 40
  p1.score = 20.9
  p1.isMan = true

  const p2 = new Person()
  p2.name = "张无忌"
  p2.age = 17
  p2.score = 69.9
  p2.isMan = true

  const p3 = new Person()
  p3.name = "赵敏"
  p3.age = 16
  p3.score = 39.9
  p3.isMan = false

  list.push(p1)
  list.push(p2)
  list.push(p3)
  
  await Rdb.saveList(list)

}

2 查

async function query() {
  // 查询Person表中所有数据
  const list: Person[] = await Rdb.findAll(Person)
  
  // 查询表中name等于张三丰的的数据  注意string和bool 类型必须是 "'参数值'" 形式
   const data: Person = await Rdb.where("name = ?", "'张三丰'")
    .findFirstOne(Person)
    
  // 查询 (年龄大于17且分数大于20) 或 性别为女 按照分数排序的数据 取前5条数据
   const list = await Rdb.where("(age > ?  AND  score > ?) OR isMan = ?", "17", "20.0", "'false'")
    .orderBy("score DESC")
    .limit("5")
    .find(Person)

}

3 改

async function update() {
  const person: Person = await Rdb.where("name = ?", "'张三丰'")
    .findFirstOne(Person)
   
    if(person){
      person.name = '张君宝'
      await person.update(person.id)
    }
  
}

4 删

async function update() {

  // 单个删除
  const person: Person = await Rdb.where("name = ?", "'张三丰'")
    .findFirstOne(Person)
    if(person){
        await person.delete(person.id)
    }
    
  // 批量删除
  const list: Person[] = ....
  await Rdb.deleteList(list)
  
  // 清空整个Person表数据
  await Rdb.clearTable(Person)
}

四 数据库升级

升级很简单,不需要手写升级迁移sql语句,只要在对象上新增或删除字段即可,然后增加数据库版本即可,新增表也是一样

如 删除 Personage 字段,新增 nickName 字段 又新增了一个新表 Book

@tableName("t_person")
export class Person extends BaseTable {
  @columnType(ColumnType.KEY_AUTO)  // 主键自增 必须
  id?: number;
  
  @columnType(ColumnType.TEXT)  // string
  name?: string;
  
  @columnType(ColumnType.TEXT) // string  
  nickName?: string;
  
  @columnType(ColumnType.REAL) // 浮点
  score?: number
  
  @columnType(ColumnType.BOOLEAN) // 布尔
  isMan?: boolean

  // 构造方法必须 全部默认值
  constructor(name: string = '', nickName: string = '',  score: number = 0.0, isMan: boolean = false ) {
    super()
    this.id = 0
    this.name = name;
    this.nickName = nickName;
    this.score = score
    this.isMan = isMan
  }
  
@tableName("t_book")
export class Book extends BaseTable {
  @columnType(ColumnType.KEY_AUTO)  // 主键自增 必须
  id?: number;
  
  @columnType(ColumnType.TEXT)  // string
  bookName?: string;

  constructor(bookName: string = '') {
    super()
    this.id = 0
    this.bookName = bookName;
  }
  

修改数据库版本号 即完成数据库升级

  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
    // oh系统默认StoreConfig
    const config: relationalStore.StoreConfig = {
      name: "my_db",
      securityLevel: relationalStore.SecurityLevel.S1,
    };
    
    const rdbConfig = new RdbConfig()
    rdbConfig.version = 2               // 数据库版本号
    rdbConfig.config = config           // 默认StoreConfig
    rdbConfig.tables = [Person,Book]    // 数据库表 新增Book表
    Rdb.init(this.context, rdbConfig)   // 初始化
  }

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0