-
Notifications
You must be signed in to change notification settings - Fork 0
/
004-选择数据库.js
48 lines (43 loc) · 1.2 KB
/
004-选择数据库.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
/*
* @Date: 2023-06-06 19:32:31
* @LastEditors: with-the-winds
* @LastEditTime: 2023-06-06 19:32:47
* @Description: 头部注释
* @FilePath: \study-node-mysql\004-选择数据库.js
*/
const mysql = require('mysql')
// 创建连接
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root123'
})
// 连接数据库
db.connect(err => {
if (err) throw err;
console.log('mysql connected ......');
})
// 创建多个数据库,方便数据库查看
const databases = ['nodemysql1', 'nodemysql2', 'nodemysql3']
databases.forEach(item => {
const createDatabaseSql = `CREATE DATABASE ${item}`
db.query(createDatabaseSql, (error, result) => {
if (error) throw error;
console.log(`Database ${item} created successfully`);
})
})
// 查看数据库列表
const showDatabaseSql = 'SHOW DATABASES'
db.query(showDatabaseSql, (error, result) => {
if (error) throw error;
console.log('List of databasets:');
result.forEach(item => {
console.log(item.Database);
})
})
// 选择要使用的数据库
const useDatabaseSql = 'USE nodemysql1'
db.query(useDatabaseSql, (error, result) => {
if (error) throw error;
console.log('Database selected successfully');
})