Node.js MySQL 建立資料庫
建立資料庫
要建立資料庫,請使用 "CREATE DATABASE" 語句。
示例
建立一個名為 "mydb" 的資料庫
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "您的使用者名稱",
密碼:"yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("已連線!");
con.query("CREATE DATABASE mydb", function (err, result) {
if (err) throw err;
console.log("資料庫已建立");
});
});
執行示例 »
將上面的程式碼儲存在一個名為 "demo_create_db.js" 的檔案中,然後執行該檔案。
執行 "demo_create_db.js"
C:\Users\你的名字>node demo_create_db.js
這將產生以下結果:
已連線!
資料庫已建立