const Database = require('better-sqlite3'); const path = require('path'); class DatabaseWrapper { constructor() { this.dbPath = process.env.DB_PATH || path.join(__dirname, '../data/database.sqlite'); this.db = new Database(this.dbPath, { verbose: console.log }); } // 查询单个记录 get(sql, params = []) { try { const stmt = this.db.prepare(sql); return stmt.get(params); } catch (error) { console.error('数据库查询错误:', error); throw error; } } // 查询多个记录 all(sql, params = []) { try { const stmt = this.db.prepare(sql); return stmt.all(params); } catch (error) { console.error('数据库查询错误:', error); throw error; } } // 执行插入、更新、删除操作 run(sql, params = []) { try { const stmt = this.db.prepare(sql); const result = stmt.run(params); return { id: result.lastInsertRowid, changes: result.changes }; } catch (error) { console.error('数据库操作错误:', error); throw error; } } // 关闭数据库连接 close() { this.db.close(); } } module.exports = new DatabaseWrapper();