Node.js: Working with MySQL
Node.js: Working with MySQL
MySQL คือ
คือ ระบบจัดการฐานข้อมูลเชิงสัมพันธ์ (Relational Database Management System หรือ RDBMS) ที่มีการจัดเก็บข้อมูลในรูปแบบของตารางข้อมูล (Table) ภายในตารางข้อมูลจะประกอบด้วยคอลัมน์ (Column) และแถวข้อมูล (Row) ในการสืบค้นและจัดการกับข้อมูลจะใช้ภาษา SQL (Structured Query Language)
เนื้อหาที่จะได้เรียนรู้จากบทเรียนนี้
- ติดตั้งโมดูล mysql
- สร้าง DataBase และ Table
- เพิ่มข้อมูลลงใน Table (Insert Data)
- เรียกดูข้อมูลจาก Table (Select Data)
ติดตั้งโมดูล mysql
เริ่มจากการติดตั้งโมดูล mysql ไว้ภายในโฟลเดอร์โปรเจคของเรา โดยใช้คำสั่ง ดังนี้
npm install mysqlเพื่อให้แน่ใจว่าการติดตั้งโมดูล mysql เสร็จสมบูรณ์ ให้ใช้คำสั่ง ดังนี้
npm ls mysqlถ้าการติดตั้งโมดูล mysql เสร็จสมบูรณ์ มันจะแสดงเวอร์ชั่นของโมดูล mysql ที่ได้ถูกติดตั้งลงไป ดังรูป
สร้าง DataBase และ Table
สร้าง DataBase ชื่อว่า “db_ijustcodeit” และ Table ชื่อว่า “person” โดยภายใน Table “person” ให้สร้างคอลัมน์ “id”, “firstname”, และ “lastname” เพื่อใช้ในการทดสอบโปรแกรม ดังนี้
เพิ่มข้อมูลลงใน Table (Insert Data)
อิมพอร์ตโมดูล MySQL
const mysql = require ( "mysql" );สร้างออปเจ็ค connection
var connectionMySQL = mysql.createConnection (
{
host : "localhost"
, user : "root"
, password : "123456"
, database : "db_ijustcodeit"
, multipleStatements : true
}
);ใช้คำสั่ง connect() ของออปเจ็ค connection เพื่อ connect ไปยัง database
connectionMySQL.connect ( function ( error )
{
if ( error )
{
console.log ( error );
}
else
{
console.log ( "MySQL Connected!" );
}
}
);เขียนคำสั่ง SQL สำหรับ insert ข้อมูล
var sqlCommand = "INSERT INTO person ( firstname, lastname ) VALUES ( ?, ? ) ";var sqlValue = [ ];
sqlValue.push ( "Somchai" );
sqlValue.push ( "Sookjai" );var sql = mysql.format ( sqlCommand, sqlValue );
connectionMySQL.query ( sql, function ( error, results, fields )
{
if ( error )
{
console.log ( error );
}
else
{
res.send ( "New Record ID is " + results.insertId );
}
}
);
สุดท้ายแล้ว คำสั่งภายในไฟล์ Node.js ก็จะเป็นดังนี้
var express = require ( "express" );const mysql = require ( "mysql" );Get I just code IT’s stories in your inbox
Join Medium for free to get updates from this writer.
var connectionMySQL = mysql.createConnection (
{
host : "localhost"
, user : "root"
, password : "123456"
, database : "db_ijustcodeit"
, multipleStatements : true
}
);connectionMySQL.connect ( function ( error )
{
if ( error )
{
console.log ( error );
}
else
{
console.log ( "MySQL Connected!" );
}
}
);
var app = express ( );app.get ( "/insertdata", function ( req, res )
{
var sqlCommand = "INSERT INTO person ( firstname, lastname ) VALUES ( ?, ? ) "; var sqlValue = [ ];
sqlValue.push ( "Somchai" );
sqlValue.push ( "Sookjai" ); var sql = mysql.format ( sqlCommand, sqlValue );
connectionMySQL.query ( sql, function ( error, results, fields )
{
if ( error )
{
console.log ( error );
}
else
{
res.send ( "New Record ID is " + results.insertId );
}
}
);
}
)
app.listen ( 8082, "127.0.0.2" );
หลังจากรันคำสั่งแล้ว ให้ทดสอบโดยการเข้าไปที่ URL “http://127.0.0.2:8082/insertdata" มันจะแสดงผล Insert ID ดังนี้
จากนั้นไปยัง “http://localhost/phpmyadmin" จะเห็นได้ว่ามี record ใหม่ถูกเพิ่มใน table
เรียกดูข้อมูลจาก Table (Select Data)
เขียนคำสั่ง SQL สำหรับ select ข้อมูล
var sql = "SELECT * FROM person";connectionMySQL.query ( sql, function ( error, results, fields )
{
if ( error )
{
console.log ( error );
}
else
{
var data = ""; results.forEach ( function ( eachRow )
{
data += eachRow.firstname + " " + eachRow.lastname;
data += "<br/>";
}
);
res.send ( data );
}
}
);
สุดท้ายแล้ว คำสั่งภายในไฟล์ Node.js ก็จะเป็นดังนี้
var express = require ( "express" );const mysql = require ( "mysql" );
var connectionMySQL = mysql.createConnection (
{
host : "localhost"
, user : "root"
, password : "123456"
, database : "db_ijustcodeit"
, multipleStatements : true
}
);connectionMySQL.connect ( function ( error )
{
if ( error )
{
console.log ( error );
}
else
{
console.log ( "MySQL Connected!" );
}
}
);
var app = express ( );app.get ( "/selectdata", function ( req, res )
{
var sql = "SELECT * FROM person"; connectionMySQL.query ( sql, function ( error, results, fields )
{
if ( error )
{
console.log ( error );
}
else
{
var data = ""; results.forEach ( function ( eachRow )
{
data += eachRow.firstname + " " + eachRow.lastname;
data += "<br/>";
}
);
res.send ( data );
}
}
);
}
)
app.listen ( 8082, "127.0.0.2" );
หลังจากรันคำสั่งแล้ว ให้ทดสอบโดยการเข้าไปที่ URL “http://127.0.0.2:8082/selectdata" มันจะแสดงผลข้อมูลที่ได้มาจากฐานข้อมูล ดังนี้









