29 lines
928 B
JavaScript
29 lines
928 B
JavaScript
const db = require('./db-sqlite');
|
|||
|
|
|
||
|
|
// 更新商品数据,将code字段中的原始型号信息提取出来存储到model字段
|
||
|
|
async function updateProducts() {
|
||
|
|
try {
|
||
|
|
// 获取所有商品
|
||
|
|
const result = await db.query('SELECT id, code FROM products');
|
||
|
|
const products = result.rows;
|
||
|
|
|
||
|
|
// 遍历商品,更新model字段
|
||
|
|
for (const product of products) {
|
||
|
|
// 提取code字段中第一个下划线之前的部分作为model
|
||
|
|
const underscoreIndex = product.code.indexOf('_');
|
||
|
|
if (underscoreIndex > 0) {
|
||
|
|
const model = product.code.substring(0, underscoreIndex);
|
||
|
|
await db.query('UPDATE products SET model = ? WHERE id = ?', [model, product.id]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('商品数据更新成功');
|
||
|
|
} catch (error) {
|
||
|
|
console.error('更新商品数据失败:', error);
|
||
|
|
} finally {
|
||
|
|
// 关闭数据库连接
|
||
|
|
db.db.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
updateProducts();
|