Sitemap
I just code IT

I just code IT

Node.js: Working with MySQL

4 min readMay 5, 2020

--

Node.js: Working with MySQL

MySQL คือ

คือ ระบบจัดการฐานข้อมูลเชิงสัมพันธ์ (Relational Database Management System หรือ RDBMS) ที่มีการจัดเก็บข้อมูลในรูปแบบของตารางข้อมูล (Table) ภายในตารางข้อมูลจะประกอบด้วยคอลัมน์ (Column) และแถวข้อมูล (Row) ในการสืบค้นและจัดการกับข้อมูลจะใช้ภาษา SQL (Structured Query Language)

เนื้อหาที่จะได้เรียนรู้จากบทเรียนนี้

  1. ติดตั้งโมดูล mysql
  2. สร้าง DataBase และ Table
  3. เพิ่มข้อมูลลงใน Table (Insert Data)
  4. เรียกดูข้อมูลจาก Table (Select Data)

ติดตั้งโมดูล mysql

เริ่มจากการติดตั้งโมดูล mysql ไว้ภายในโฟลเดอร์โปรเจคของเรา โดยใช้คำสั่ง ดังนี้

npm install mysql

เพื่อให้แน่ใจว่าการติดตั้งโมดูล mysql เสร็จสมบูรณ์ ให้ใช้คำสั่ง ดังนี้

npm ls mysql

ถ้าการติดตั้งโมดูล mysql เสร็จสมบูรณ์ มันจะแสดงเวอร์ชั่นของโมดูล mysql ที่ได้ถูกติดตั้งลงไป ดังรูป

Press enter or click to view image in full size

สร้าง DataBase และ Table

สร้าง DataBase ชื่อว่า “db_ijustcodeit” และ Table ชื่อว่า “person” โดยภายใน Table “person” ให้สร้างคอลัมน์ “id”, “firstname”, และ “lastname” เพื่อใช้ในการทดสอบโปรแกรม ดังนี้

Press enter or click to view image in full size

เพิ่มข้อมูลลงใน 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 ก็จะเป็นดังนี้

Get I just code IT’s stories in your inbox

Join Medium for free to get updates from this writer.

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 ( "/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 ดังนี้

Press enter or click to view image in full size

จากนั้นไปยัง “http://localhost/phpmyadmin" จะเห็นได้ว่ามี record ใหม่ถูกเพิ่มใน table

Press enter or click to view image in full size

เรียกดูข้อมูลจาก 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" มันจะแสดงผลข้อมูลที่ได้มาจากฐานข้อมูล ดังนี้

Press enter or click to view image in full size

สามารถศึกษาบทความนี้ผ่านวิดีโอได้ ดังนี้

--

--