半模态转场来实现弹框样式的页面

在数据分析和处理的过程中,经常需要对数据库中的数据进行计算和分析。例如,在某个销售数据表中,我们需要计算每个销售人员的平均销售额。为了实现这个功能,我们可以使用数据库的查询语句来读取每个销售人员的销售额数据,并进行求和运算,最后再除以销售人员的总数,得到平均销售额。对标sql语句中的avg函数运算,该种方法未来有更多的可拓展性,比如计算方差,重数等等。

HarmonyOS
2024-05-22 23:18:02
1205浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
人提唱盘

核心代码解释:Average函数为主要的调动函数,通过读取某一行的数据,将所有数据累加与个数相除,将所得到数据存入全局变量中,最后前端调用变量即可获得平均值。后续也可在这部分进行更改,获得sql无法实现的其他运算。

实现效果

通过读取数据库的某行数据,进行全面搜索统计,之后进行平均计算。

import relationalStore from '@ohos.data.relationalStore'; 
import ItemBean from './ItemBean'; 
import { ValuesBucket } from '@ohos.data.ValuesBucket'; 
 
export default class RdbTest { 
  //  rdbStore:relationalStore.RdbStore = undefined; 
 
  rdbStore?: relationalStore.RdbStore; 
  context: Context = getContext(this); 
  //  dataSet:relationalStore.ResultSet = undefined; 
  dataSet?: relationalStore.ResultSet; 
  dbSet: Array<ItemBean> = []; 
  sum: number = 0.0 
  i: number = 0.0 
 
  MyGetRdbStore() { 
 
    interface StoreConfig { 
      name: string; 
      securityLevel: relationalStore.SecurityLevel; 
    } 
 
    const STORE_CONFIG: StoreConfig = { 
      name: 'RdbTest.db', 
      securityLevel: relationalStore.SecurityLevel.S1 
    }; 
 
 
    relationalStore.getRdbStore(this.context, STORE_CONFIG, (err, store) => { 
      if (err) { 
        console.error(`Failed to get RdbStore. Code:${err.code}, message:${err.message}`); 
        return; 
      } else { 
        console.info(`成功获取 RdbStore.`); 
      } 
      this.rdbStore = store; 
      this.CreateTable(); 
    } 
 
    ) 
 
 
  } 
 
  Average() { 
    let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 
    predicates.inAllDevices(); 
    if (this.rdbStore != undefined) { 
      this.rdbStore.query(predicates, ['ID', 'NAME'], (err, resultSet) => { 
        if (err) { 
          console.error(`Failed to query data. Code:${err.code}, message:${err.message}`); 
          return; 
        } 
        console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 
        // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 
        this.dbSet = []; 
        while (resultSet.goToNextRow()) { 
          const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 
          const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 
 
          const num = new Number(name).valueOf(); 
          this.sum = this.sum + num; 
          this.i++; 
        } 
        // 释放数据集的内存 
        this.sum = this.sum / this.i; 
        console.log("!!!" + this.sum); 
        this.dataSet = resultSet; 
        resultSet.close(); 
      }) 
    } 
  } 
 
  CreateTable() { 
    if (this.rdbStore != undefined) { 
 
      const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT NOT NULL)'; 
      this.rdbStore.executeSql(SQL_CREATE_TABLE); 
      console.log("成功建表") 
    } 
  } 
 
  InsertData(name: number) { 
    // 插入数据 
 
 
    const valueBucket: ValuesBucket = { 
      'NAME': name 
    }; 
 
 
    if (this.rdbStore != undefined) { 
 
      this.rdbStore.insert('EMPLOYEE', valueBucket, (err, rowId) => { 
        if (err) { 
          console.error(`Failed to insert data. Code:${err.code}, message:${err.message}`); 
          return; 
        } 
        console.info(`Succeeded in inserting data. rowId:${rowId}`); 
      }) 
    } 
 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
分享
微博
QQ
微信
回复
2024-05-23 18:12:54


相关问题
HarmonyOS 模态转场
936浏览 • 1回复 待解决
如何固定模态转场高度
1416浏览 • 1回复 待解决
HarmonyOS 关于模态转场疑问
870浏览 • 1回复 待解决
模态转场如何控制固定高度
2728浏览 • 1回复 待解决
HarmonyOS 模态转场,如何透传手势?
1207浏览 • 1回复 待解决
页面类似样式跳转H5
729浏览 • 1回复 待解决
应用怎么实现模态效果
3006浏览 • 1回复 待解决