forked from 2662419405/AllDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql2.js
37 lines (29 loc) · 872 Bytes
/
mysql2.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
(async () => {
const mysql = require('mysql2/promise')
// 连接配置
const cfg = {
host: "localhost",
user: "root",
password: "example", // 修改为你的密码
database: "kaikeba" // 请确保数据库存在
}
const connection = await mysql.createConnection(cfg)
let ret = await connection.execute(`
CREATE TABLE IF NOT EXISTS test (
id INT NOT NULL AUTO_INCREMENT,
message VARCHAR(45) NULL,
PRIMARY KEY (id))
`)
console.log('create', ret)
ret = await connection.execute(`
INSERT INTO test(message)
VALUES(?)
`, ['ABC'])
console.log('insert:', ret)
ret = await connection.execute(`
SELECT * FROM test
`)
console.log(JSON.stringify(ret[0]))
// console.log(ret[1])
connection.end()
})()