-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.ts
38 lines (32 loc) · 1.13 KB
/
Database.ts
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
import mysql from "mysql2/promise";
export class Database {
async test(): Promise<void> {
// Create the connection to database
const connection = await mysql.createConnection({
host: "localhost",
user: "root",
database: "test",
});
// A simple SELECT query
try {
const [results, fields] = await connection.query(
'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
);
console.log(results); // results contains rows returned by server
console.log(fields); // fields contains extra meta data about results, if available
} catch (err) {
console.log(err);
}
// A simple INSERT execution
try {
const sql =
"INSERT INTO `users`(`name`, `age`) VALUES (?, ?), (?,?)";
const values = ["Josh", 19, "Page", 45];
const [result, fields] = await connection.execute(sql, values);
console.log(result);
console.log(fields);
} catch (err) {
console.log(err);
}
}
}