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') | ||||||
|       } |       } | ||||||
|     }) |     }) | ||||||
| @ -213,7 +218,7 @@ class DbUtils { | |||||||
|         obj[info.name] = val; |         obj[info.name] = val; | ||||||
|       }); |       }); | ||||||
|       // 3.3.将对象填入结果数组 |       // 3.3.将对象填入结果数组 | ||||||
|       arr.push(obj as ESObject as  T); // 将 obj 断言为 T 类型 |       arr.push(obj as ESObject as T); // 将 obj 断言为 T 类型 | ||||||
|     } |     } | ||||||
|     return arr; |     return arr; | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -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,28 +137,34 @@ 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) { | ||||||
|         let arrList: Array<RecordType> | RecordType = [] |           console.log("可以插入的表", key, CenterMap.get(key)) | ||||||
|         let value: PublicInfoType = Reflect.get(res.initializationRsp.body, key) |           let arrList: Array<RecordType> | RecordType = [] | ||||||
|         if (value.record instanceof Array) { |           let value: PublicInfoType = Reflect.get(res.initializationRsp.body, key) | ||||||
|           arrList = value.record |           if (value.record instanceof Array) { | ||||||
|         } else { |             arrList = value.record | ||||||
|           arrList.push(value.record) |           } else { | ||||||
|         } |             arrList.push(value.record) | ||||||
|         const folderPath = await fileUtil.initFolder(`/config/tableList`); |           } | ||||||
|         fileUtil.addFile(`${folderPath}/${RemappingTableName.get(key)}.txt`, JSON.stringify(arrList)) |           const folderPath = await fileUtil.initFolder(`/config/tableList`); | ||||||
|         try { |           fileUtil.addFile(`${folderPath}/${RemappingTableName.get(key)}.txt`, JSON.stringify(arrList)) | ||||||
|           const result = await SqlInsertTable(RemappingTableName.get(key)!, arrList); |           try { | ||||||
|           if (!result) { |             console.log("插入表操作", key) | ||||||
|             Prompt.showToast({ |             console.log("插入表别名", RemappingTableName.get(key)) | ||||||
|               message: '初始化数据库失败' |             console.log("插入表长度", arrList.length) | ||||||
|             }) |             const result = await SqlInsertTable(RemappingTableName.get(key)!, arrList); | ||||||
|  |             console.log("插入表结果,表", key, "结果", result) | ||||||
|  |             if (!result) { | ||||||
|  |               Prompt.showToast({ | ||||||
|  |                 message: '初始化数据库失败' | ||||||
|  |               }) | ||||||
|  |             } | ||||||
|  |             resolve(result) | ||||||
|  |           } catch (e) { | ||||||
|  |             reject(e) | ||||||
|           } |           } | ||||||
|           resolve(result) |  | ||||||
|         } catch (e) { |  | ||||||
|           reject(e) |  | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user