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