fix: 修改插入函数
This commit is contained in:
parent
2a6eb111a3
commit
f289dd29d8
@ -173,6 +173,8 @@ struct Index {
|
|||||||
} else {
|
} else {
|
||||||
this.customDialogController.close()
|
this.customDialogController.close()
|
||||||
}
|
}
|
||||||
|
}).catch((err: BusinessError) => {
|
||||||
|
console.error("初始化表失败", err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -63,9 +63,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)
|
||||||
resolve()
|
resolve()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
console.error(DbTag, "sql clearTable fail", sql)
|
||||||
reject('rdbStore is null')
|
reject('rdbStore is null')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -76,11 +78,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)
|
||||||
resolve("")
|
resolve("")
|
||||||
}).catch((err: BusinessError) => {
|
}).catch((err: BusinessError) => {
|
||||||
|
console.error(DbTag, `sql executeSql fail err:${JSON.stringify(err)}`);
|
||||||
reject(err)
|
reject(err)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
console.error(DbTag, "sql executeSql fail", sql)
|
||||||
reject('rdbStore is null')
|
reject('rdbStore is null')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@ -10,12 +10,16 @@ import FileUtils from '../FileUtils';
|
|||||||
|
|
||||||
// 建表操作
|
// 建表操作
|
||||||
export async function InitTable() {
|
export async function InitTable() {
|
||||||
Object.keys(ParameterPlatform).forEach(async (item) => {
|
ParameterPlatform.forEach(item => {
|
||||||
const platformItem = ParameterPlatform.get(item);
|
console.log("表名", item.sqlCreate)
|
||||||
if (platformItem) {
|
if (item) {
|
||||||
await DB.executeSql(platformItem.sqlCreate);
|
DB.executeSql(item.sqlCreate).then(() => {
|
||||||
|
console.log("表创建成功", item.tableName)
|
||||||
|
}).catch((err: BusinessError) => {
|
||||||
|
console.log("表创建失败", item.tableName, err)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 插表操作
|
// 插表操作
|
||||||
@ -25,21 +29,22 @@ export function SqlInsertTable(tableName: string, data: Array<RecordType>, delFl
|
|||||||
element.id = !delFlag ? `${index + data.length}` : `${index}`
|
element.id = !delFlag ? `${index + data.length}` : `${index}`
|
||||||
})
|
})
|
||||||
DB.clearTable(tableName).then((res) => {
|
DB.clearTable(tableName).then((res) => {
|
||||||
const columns = Object.keys(data[0]).join(', ');
|
const columnsData = ParameterPlatform.get(tableName)?.columns
|
||||||
const values = data
|
const columns: string = columnsData?.filter((column) => column !== "id").join(",") || "";
|
||||||
.map((item) => {
|
const values = data.map(item => {
|
||||||
const rowValues = Object.values(item)
|
const value = columnsData?.filter(column => column !== "id")
|
||||||
.map((value: string | number) => (typeof value === 'string' ? `'${value}'` : value))
|
.map((column) => `'${Reflect.get(item, column) || ""}'`)
|
||||||
return `(${rowValues})`;
|
.join(",")
|
||||||
|
return value
|
||||||
})
|
})
|
||||||
.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)
|
||||||
DB.executeSql(INSERT_SQL).then(() => {
|
DB.executeSql(INSERT_SQL).then(() => {
|
||||||
console.log('sql insert,', res, 'tableName', tableName)
|
console.log('插入表成功', 'tableName', tableName)
|
||||||
resolve(true)
|
resolve(true)
|
||||||
}).catch((err: BusinessError) => {
|
}).catch((err: BusinessError) => {
|
||||||
console.log('sql insert err,', JSON.stringify(err), 'tableName', tableName)
|
console.log('插入表失败,', JSON.stringify(err), 'tableName', tableName)
|
||||||
reject(err)
|
reject(err)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -114,7 +119,7 @@ export async function InitializeTheCentralTable(params: InitializeTheCentralTabl
|
|||||||
resolve(flag)
|
resolve(flag)
|
||||||
}
|
}
|
||||||
// 非单机模式
|
// 非单机模式
|
||||||
if (params.carId) {
|
if (!params.carId) {
|
||||||
resolve(false)
|
resolve(false)
|
||||||
}
|
}
|
||||||
const str: InitializeTheCentralTableType = {
|
const str: InitializeTheCentralTableType = {
|
||||||
@ -132,9 +137,10 @@ export async function InitializeTheCentralTable(params: InitializeTheCentralTabl
|
|||||||
}
|
}
|
||||||
if (res.initializationRsp && res.initializationRsp.body) {
|
if (res.initializationRsp && res.initializationRsp.body) {
|
||||||
for (const key of Object.keys(res.initializationRsp.body)) {
|
for (const key of Object.keys(res.initializationRsp.body)) {
|
||||||
if (CenterMap[key]) {
|
let isExit = CenterMap.has(key)
|
||||||
continue
|
console.log("表名", key, isExit)
|
||||||
}
|
if (isExit) {
|
||||||
|
console.log("可以插入的表", key, CenterMap.get(key))
|
||||||
let arrList: Array<RecordType> | RecordType = []
|
let arrList: Array<RecordType> | RecordType = []
|
||||||
let value: PublicInfoType = Reflect.get(res.initializationRsp.body, key)
|
let value: PublicInfoType = Reflect.get(res.initializationRsp.body, key)
|
||||||
if (value.record instanceof Array) {
|
if (value.record instanceof Array) {
|
||||||
@ -145,7 +151,11 @@ export async function InitializeTheCentralTable(params: InitializeTheCentralTabl
|
|||||||
const folderPath = await fileUtil.initFolder(`/config/tableList`);
|
const folderPath = await fileUtil.initFolder(`/config/tableList`);
|
||||||
fileUtil.addFile(`${folderPath}/${RemappingTableName.get(key)}.txt`, JSON.stringify(arrList))
|
fileUtil.addFile(`${folderPath}/${RemappingTableName.get(key)}.txt`, JSON.stringify(arrList))
|
||||||
try {
|
try {
|
||||||
|
console.log("插入表操作", key)
|
||||||
|
console.log("插入表别名", RemappingTableName.get(key))
|
||||||
|
console.log("插入表长度", arrList.length)
|
||||||
const result = await SqlInsertTable(RemappingTableName.get(key)!, arrList);
|
const result = await SqlInsertTable(RemappingTableName.get(key)!, arrList);
|
||||||
|
console.log("插入表结果,表", key, "结果", result)
|
||||||
if (!result) {
|
if (!result) {
|
||||||
Prompt.showToast({
|
Prompt.showToast({
|
||||||
message: '初始化数据库失败'
|
message: '初始化数据库失败'
|
||||||
@ -157,6 +167,7 @@ export async function InitializeTheCentralTable(params: InitializeTheCentralTabl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user