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 "CREATE DATABASE":
Ví 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");
});
});
Chạy ví dụ »Lưu mã ở trên vào 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ẽ cho bạn kết quả này:
Connected!
Database created