Node.js MySQL Tạo cơ sở dữ liệu
Tạo cơ sở dữ liệu
Để tạo cơ sở dữ liệu trong MySQL, hãy sử dụng câu lệnh "TẠO CƠ SỞ DỮ LIỆU":
Thí dụ
Tạo cơ sở dữ liệu có tên "mydb":
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE mydb", function (err, result) {
if (err) throw err;
console.log("Database
created");
});
});
Lưu mã ở trên vào một tệp có tên "demo_create_db.js" và chạy tệp:
Chạy "demo_create_db.js"
C:\Users\Your Name>node demo_create_db.js
Điều này sẽ cung cấp cho bạn kết quả này:
Connected!
Database created