更新到数据库
This commit is contained in:
19
put_database.js
Normal file
19
put_database.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import mysql from 'mysql2/promise'
|
||||
import fs from 'fs'
|
||||
|
||||
export async function updateDatabase(list, field) {
|
||||
const { mysql: config } = JSON.parse(fs.readFileSync('config.json', 'utf8'))
|
||||
const conn = await mysql.createConnection(config)
|
||||
|
||||
await conn.query(`CREATE TEMPORARY TABLE temp_updates (id INT PRIMARY KEY, ${field} INT)`)
|
||||
|
||||
for (let i = 0; i < list.length; i += 10000) {
|
||||
const values = list.slice(i, i + 10000).map(({ id, ...rest }) => [id, rest[field]])
|
||||
await conn.query(`INSERT INTO temp_updates (id, ${field}) VALUES ?`, [values])
|
||||
}
|
||||
|
||||
await conn.query(`UPDATE web_images t JOIN temp_updates tu ON t.id = tu.id SET t.${field} = tu.${field}`)
|
||||
console.log('更新完成')
|
||||
await conn.end()
|
||||
}
|
||||
|
Reference in New Issue
Block a user