You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.9 KiB
59 lines
1.9 KiB
1 year ago
|
try {
|
||
|
const { Pool, Client } = require('pg')
|
||
|
const XLSX = require('xlsx')
|
||
|
const path = require('path')
|
||
|
const fs = require("fs");
|
||
|
|
||
|
// 测试环境
|
||
|
const pool = new Pool({
|
||
|
user: 'postgres',
|
||
|
host: '10.8.30.32',
|
||
|
database: 'highways4good',
|
||
|
password: '123',
|
||
|
port: 5432,
|
||
|
})
|
||
|
|
||
|
const fun = async () => {
|
||
|
// note: we don't try/catch this because if connecting throws an exception
|
||
|
// we don't need to dispose of the client (it will be undefined)
|
||
|
const client = await pool.connect()
|
||
|
try {
|
||
|
await client.query('BEGIN')
|
||
|
console.log(`开始`);
|
||
|
|
||
|
// 读取数据文件
|
||
|
let workbook = XLSX.readFile(path.join(__dirname, './data/乡道(新)(1).xlsx'));
|
||
|
let firstSheetName = workbook.SheetNames[0];
|
||
|
let worksheet = workbook.Sheets[firstSheetName];
|
||
|
let res = XLSX.utils.sheet_to_json(worksheet, {
|
||
|
defval: ''
|
||
|
});
|
||
|
|
||
|
for (let r of res) {
|
||
|
console.log(r);
|
||
|
fs.writeFileSync('AnswerOld.sql', `;\n`, 'utf-8');
|
||
|
// let time = generateRandomTimeString()
|
||
|
// console.log(time);
|
||
|
// await client.query(
|
||
|
// `INSERT INTO report (report_type, project_type, road, time,content,user_id) VALUES($1, $2, $3, $4, $5, $6) `,
|
||
|
// ['conserve', 'road', r['养护道路'], time, r['养护内容'], userId])
|
||
|
// // break
|
||
|
}
|
||
|
|
||
|
// await client.query('ROLLBACK')
|
||
|
await client.query('COMMIT')
|
||
|
console.log('执行完毕~')
|
||
|
} catch (e) {
|
||
|
await client.query('ROLLBACK')
|
||
|
console.log('执行错误~')
|
||
|
throw e
|
||
|
} finally {
|
||
|
client.release();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fun()
|
||
|
} catch (error) {
|
||
|
console.error(error)
|
||
|
}
|