fix: 优化数据库方面操作打印信息

This commit is contained in:
wangzhongjie 2025-06-25 13:48:16 +08:00
parent 014c21ad81
commit 9da3b0f856
3 changed files with 35 additions and 28 deletions

View File

@ -50,3 +50,6 @@ export const JudgeTag = '[Judge]';
//初始化表 //初始化表
export const InitTableTag = '[InitTable]'; export const InitTableTag = '[InitTable]';
//数据库操作
export const DbOperationTag = '[DbOperation]';

View File

@ -2,6 +2,7 @@ import relationalStore from '@ohos.data.relationalStore'
import common from '@ohos.app.ability.common' import common from '@ohos.app.ability.common'
import { BusinessError } from '@ohos.base' import { BusinessError } from '@ohos.base'
import { DbTag } from '../config' import { DbTag } from '../config'
import { dConsole } from './LogWorker'
export interface ColumnInfo { export interface ColumnInfo {
name: string, name: string,
@ -27,11 +28,11 @@ class DbUtils {
relationalStore.getRdbStore(context, config) relationalStore.getRdbStore(context, config)
.then(rdbStore => { .then(rdbStore => {
this.rdbStore = rdbStore this.rdbStore = rdbStore
console.log(DbTag, "db rdbStore init success") dConsole.log(DbTag, "db rdbStore init success")
resolve() resolve()
}) })
.catch((err: BusinessError) => { .catch((err: BusinessError) => {
console.error(DbTag, `db rdbStore init fail reason:${err}`); dConsole.error(DbTag, `db rdbStore init fail reason:${err}`);
reject(err) reject(err)
}) })
@ -44,11 +45,11 @@ class DbUtils {
if (this.rdbStore) { if (this.rdbStore) {
this.rdbStore?.executeSql(createSql) this.rdbStore?.executeSql(createSql)
.then(() => { .then(() => {
console.log(DbTag, "sql createTable success") dConsole.log(DbTag, "sql createTable success")
resolve() resolve()
}) })
.catch((err: BusinessError) => { .catch((err: BusinessError) => {
console.error(DbTag, `sql createTable fail err:${JSON.stringify(err)}`); dConsole.error(DbTag, `sql createTable fail err:${JSON.stringify(err)}`);
reject(err) reject(err)
}) })
} else { } else {
@ -63,11 +64,11 @@ class DbUtils {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.rdbStore) { if (this.rdbStore) {
this.rdbStore?.executeSql(sql).then(() => { this.rdbStore?.executeSql(sql).then(() => {
console.log(DbTag, "sql clearTable success", sql) dConsole.log(DbTag, "sql clearTable success", sql)
resolve() resolve()
}) })
} else { } else {
console.error(DbTag, "sql clearTable fail", sql) dConsole.error(DbTag, "sql clearTable fail", sql)
reject('rdbStore is null') reject('rdbStore is null')
} }
}) })
@ -78,14 +79,14 @@ class DbUtils {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.rdbStore) { if (this.rdbStore) {
this.rdbStore?.executeSql(sql).then(() => { this.rdbStore?.executeSql(sql).then(() => {
console.log(DbTag, "sql executeSql success", sql) dConsole.log(DbTag, "sql executeSql success", sql)
resolve("") resolve("")
}).catch((err: BusinessError) => { }).catch((err: BusinessError) => {
console.error(DbTag, `sql executeSql fail err:${JSON.stringify(err)}`); dConsole.error(DbTag, `sql executeSql fail err:${JSON.stringify(err)}`);
reject(err) reject(err)
}) })
} else { } else {
console.error(DbTag, "sql executeSql fail", sql) dConsole.error(DbTag, "sql executeSql fail", sql)
reject('rdbStore is null') reject('rdbStore is null')
} }
}) })
@ -97,10 +98,10 @@ class DbUtils {
if (this.rdbStore) { if (this.rdbStore) {
this.rdbStore?.insert(tableName, obj, (err, res) => { this.rdbStore?.insert(tableName, obj, (err, res) => {
if (err) { if (err) {
console.error(DbTag, `sql insertData fail err:${JSON.stringify(err)}`); dConsole.error(DbTag, `sql insertData fail err:${JSON.stringify(err)}`);
reject(err) reject(err)
} else { } else {
console.log(DbTag, `sql insertData success res:${res}`); dConsole.log(DbTag, `sql insertData success res:${res}`);
resolve(res) resolve(res)
} }
}) })
@ -115,10 +116,10 @@ class DbUtils {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const SQL = `DELETE FROM ${tableName};` const SQL = `DELETE FROM ${tableName};`
this.executeSql(SQL).then(res => { this.executeSql(SQL).then(res => {
console.log(DbTag, "删除表成功!表名:", tableName) dConsole.log(DbTag, "删除表成功!表名:", tableName)
resolve(true) resolve(true)
}).catch((err: BusinessError) => { }).catch((err: BusinessError) => {
console.error(DbTag, "删除表失败!表名:", tableName, "错误信息:", JSON.stringify(err)) dConsole.error(DbTag, "删除表失败!表名:", tableName, "错误信息:", JSON.stringify(err))
reject(false) reject(false)
}) })
}) })
@ -137,7 +138,7 @@ class DbUtils {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.rdbStore) { if (this.rdbStore) {
this.rdbStore.querySql(sql, []).then((res: relationalStore.ResultSet) => { this.rdbStore.querySql(sql, []).then((res: relationalStore.ResultSet) => {
console.log(DbTag, "sql query", JSON.stringify(res)) dConsole.log(DbTag, "sql query", JSON.stringify(res))
if (res.rowCount <= 0) { if (res.rowCount <= 0) {
resolve(0 as T) resolve(0 as T)
} else { } else {
@ -145,7 +146,7 @@ class DbUtils {
resolve(res.getLong(0) as T) resolve(res.getLong(0) as T)
} }
}).catch((err: BusinessError) => { }).catch((err: BusinessError) => {
console.error(DbTag, `sql queryCount fail err:${JSON.stringify(err)}`); dConsole.error(DbTag, `sql queryCount fail err:${JSON.stringify(err)}`);
reject(err) reject(err)
}) })
} else { } else {
@ -160,10 +161,10 @@ class DbUtils {
if (this.rdbStore) { if (this.rdbStore) {
this.rdbStore?.query(predicates, columns.map(info => info.columnName), (err, result) => { this.rdbStore?.query(predicates, columns.map(info => info.columnName), (err, result) => {
if (err) { if (err) {
console.error(DbTag, `sql queryForList fail err:${JSON.stringify(err)}`); dConsole.error(DbTag, `sql queryForList fail err:${JSON.stringify(err)}`);
reject(err) reject(err)
} else { } else {
console.log(DbTag, `sql queryForList success rows: ${result.rowCount.toString()}`) dConsole.log(DbTag, `sql queryForList success rows: ${result.rowCount.toString()}`)
resolve(this.parseResultSet(result, columns)) resolve(this.parseResultSet(result, columns))
} }
}) })
@ -179,7 +180,7 @@ class DbUtils {
if (this.rdbStore) { if (this.rdbStore) {
this.rdbStore?.querySql(sql, [], (err, result) => { this.rdbStore?.querySql(sql, [], (err, result) => {
if (err) { if (err) {
console.error(DbTag, `sql queryForListBySql fail err:${JSON.stringify(err)}`); dConsole.error(DbTag, `sql queryForListBySql fail err:${JSON.stringify(err)}`);
reject(err) reject(err)
} else { } else {
resolve(this.parseResultSet(result, columns)) resolve(this.parseResultSet(result, columns))
@ -229,11 +230,11 @@ class DbUtils {
if (this.rdbStore) { if (this.rdbStore) {
this.rdbStore?.delete(predicates) this.rdbStore?.delete(predicates)
.then(rows => { .then(rows => {
console.log(DbTag, `DbUtil delete success rows: ${rows.toString()}`) dConsole.log(DbTag, `DbUtil delete success rows: ${rows.toString()}`)
resolve(rows) resolve(rows)
}) })
.catch((err: BusinessError) => { .catch((err: BusinessError) => {
console.error(DbTag, `DbUtil delete fail err:${JSON.stringify(err)}`); dConsole.error(DbTag, `DbUtil delete fail err:${JSON.stringify(err)}`);
reject(err) reject(err)
}) })
} else { } else {

View File

@ -5,19 +5,19 @@ import { BusinessError } from '@ohos.base';
import Prompt from '@system.prompt'; import Prompt from '@system.prompt';
import { initialization } from '../../api'; import { initialization } from '../../api';
import { GlobalConfig, InitTableTag } from '../../config'; import { DbOperationTag, GlobalConfig, InitTableTag } from '../../config';
import FileUtils from '../FileUtils'; import FileUtils from '../FileUtils';
import { dConsole } from '../LogWorker'; import { dConsole } from '../LogWorker';
// 建表操作 // 建表操作
export async function InitTable() { export async function InitTable() {
ParameterPlatform.forEach(item => { ParameterPlatform.forEach(item => {
console.log("表名", item.sqlCreate) dConsole.log(DbOperationTag, "表名", item.sqlCreate)
if (item) { if (item) {
DB.executeSql(item.sqlCreate).then(() => { DB.executeSql(item.sqlCreate).then(() => {
console.log("表创建成功", item.tableName) dConsole.log(DbOperationTag, "表创建成功", item.tableName)
}).catch((err: BusinessError) => { }).catch((err: BusinessError) => {
console.log("表创建失败", item.tableName, err) dConsole.error(DbOperationTag, "表创建失败", item.tableName, err)
}) })
} }
}) })
@ -40,12 +40,12 @@ export function SqlInsertTable(tableName: string, data: Array<RecordType>, delFl
}).join(",") }).join(",")
let INSERT_SQL = "INSERT INTO " + tableName let INSERT_SQL = "INSERT INTO " + tableName
+ " (" + columns + ") VALUES " + values + " (" + columns + ") VALUES " + values
console.log('表插入语句,', INSERT_SQL, 'tableName', tableName) dConsole.log(DbOperationTag, '表插入语句,', INSERT_SQL, 'tableName', tableName)
DB.executeSql(INSERT_SQL).then(() => { DB.executeSql(INSERT_SQL).then(() => {
console.log('插入表成功', 'tableName', tableName) dConsole.log(DbOperationTag, '插入表成功', 'tableName', tableName)
resolve(true) resolve(true)
}).catch((err: BusinessError) => { }).catch((err: BusinessError) => {
console.log('插入表失败,', JSON.stringify(err), 'tableName', tableName) dConsole.error(DbOperationTag, '插入表失败,', JSON.stringify(err), 'tableName', tableName)
reject(err) reject(err)
}) })
}) })
@ -67,6 +67,7 @@ export async function GetSyncData<T>(tableName: string): Promise<T[]> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const platform = ParameterPlatform.get(tableName); const platform = ParameterPlatform.get(tableName);
if (!platform || !platform.columns) { if (!platform || !platform.columns) {
dConsole.error(DbOperationTag, `Invalid tableName or columns not found for table: ${tableName}`);
throw new Error(`Invalid tableName or columns not found for table: ${tableName}`); throw new Error(`Invalid tableName or columns not found for table: ${tableName}`);
} }
const columns: ColumnInfo[] = platform.columns.map((res: string) => { const columns: ColumnInfo[] = platform.columns.map((res: string) => {
@ -77,8 +78,10 @@ export async function GetSyncData<T>(tableName: string): Promise<T[]> {
} as ColumnInfo; } as ColumnInfo;
}); });
DB.queryListBySql<T>(`select * from ${tableName}`, columns).then((res: T[]) => { DB.queryListBySql<T>(`select * from ${tableName}`, columns).then((res: T[]) => {
dConsole.log(DbOperationTag, `获取表 ${tableName} 数据成功`, res);
resolve(res); resolve(res);
}).catch((err: BusinessError) => { }).catch((err: BusinessError) => {
dConsole.error(DbOperationTag, `获取表 ${tableName} 数据失败`, JSON.stringify(err));
reject(err); reject(err);
}); });
}); });