-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathdatabase.js
81 lines (65 loc) · 2.07 KB
/
database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 1️⃣ 导入时用别名重命名原模块的 Database
import pkg from 'node-sqlite3-wasm';
const {Database: SQLite3Database} = pkg; // 👈 关键别名
import {fileURLToPath} from "url";
import path from 'path';
// 2️⃣ 定义你的自定义类(可继承/扩展/完全重写)
export class DataBase {
constructor(db_file) {
this.db_file = db_file || './database.db';
this.db = null;
}
// 自定义方法
async initDb() {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const __rootPath = path.join(__dirname, '../');
const __dbpath = path.join(__rootPath, this.db_file);
// console.log('__dbpath:', __dbpath);
const db = new SQLite3Database(__dbpath);
this.db = db;
return db
}
async startDb() {
if (!this.db) {
await this.initDb()
}
}
async endDb() {
if (this.db) {
await this.db.close();
this.db = null;
}
}
}
async function main() {
// 打开数据库(若不存在则创建)
const db = new SQLite3Database("../database.db");
// 创建表
db.run(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
)
`);
// 插入数据
db.run('INSERT INTO users (name) VALUES (?)', ['Alice']);
db.run('INSERT INTO users (name) VALUES (?)', ['Bob']);
// 查询数据
const users = db.all('SELECT * FROM users');
console.log(users);
// 更新数据
db.run('UPDATE users SET name = ? WHERE id = ?', ['Charlie', 1]);
// 查询更新后的数据
const updatedUsers = db.all('SELECT * FROM users');
console.log(updatedUsers);
// 删除数据
db.run('DELETE FROM users WHERE id = ?', [2]);
// 查询删除后的数据
const finalUsers = db.all('SELECT * FROM users');
console.log(finalUsers);
// 关闭数据库
db.close();
}
export const database = new DataBase('./database.db');
// main().catch(err => console.error(err));