Node.jsMySQLデータベースの作成


データベースの作成

MySQLでデータベースを作成するには、「CREATEDATABASE」ステートメントを使用します。

「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");
  });
});

上記のコードを「demo_create_db.js」というファイルに保存し、ファイルを実行します。

「demo_create_db.js」を実行します

C:\Users\Your Name>node demo_create_db.js

これはあなたにこの結果を与えるでしょう:

Connected!
Database created