Node.js: 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":
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 nodejs_db", function (err, result) { if (err) throw err; console.log("Database created"); }); });
Lưu mã ở trên vào tệp có tên "demo_create_db.js" và chạy tệp:
C:\Users\Your Name>node demo_create_db.js
Kết quả:
Connected!
Database created
Database created