-
06/15 DB update, delete 및 node.js서버 DB검색 구현데이터베이스 2021. 6. 15. 13:13
UPDATE(수정)
UPDATE 테이블명 SET 컬럼명 = '바꿀내용' where 바꿀 대상을 취득하는 조건식; //ex) where id = 2;
DELETE (삭제)
DELETE FROM 테이블 WHERE 조건식;
node.js mysql 모듈
1. mysql
https://www.npmjs.com/package/mysql
mysql
A node.js driver for mysql. It is written in JavaScript, does not require compiling, and is 100% MIT licensed.
www.npmjs.com
2. mysql2
https://www.npmjs.com/package/mysql2
mysql2
fast mysql driver. Implements core protocol, prepared statements, ssl and compression in native JS
www.npmjs.com
시퀄라이저를 사용할 수 있음
node.js 프로젝트 생성
product-express
npm init -y
npm i mysql
npm i express
npm i supervisor
서버 코드 변경시 자동 재시작 모듈
현재 설치 모듈
express
mysql
supervisor
DB 테이블 생성
mysql workbench 사용
또는
sql
CREATE TABLE `products` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(45) DEFAULT NULL, `category` varchar(45) DEFAULT NULL, `price` int DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3
데이터 추가
1 [현대지성]아라비안 나이트(현대지성 클래식 8) (천일야화) 동양 고전문학 13000 node.js 서버 코드
app.js
const express = require('express'); const app = express(); const port = 3000; const mysql = require('mysql2'); const connection = mysql.createConnection({ host : '127.0.0.1', port : '3307', user : 'root', password : '1234', database : 'nodejs' }); app.get('/', (req, res) => { res.end('hello express'); }); app.get('/:id', (req, res) => { const id = req.params.id; const query = `select * from products where id = ${id}`; console.log(query); connection.execute(query, (err, results, fiedls) => { if(err) throw err; const result = { status: 200, results, } //results.forEach(product => console.log(product.id, product.name)); const json = JSON.stringify(result); res.send(json); }); }); app.get('/dbconnect', (req, res) => { console.log('conncetion :' + connection); res.end('connectioin test'); }); app.listen(port, () => { console.log(`server is running at ${port} port.`); });
'데이터베이스' 카테고리의 다른 글
07/13 데이터베이스 기초1 (0) 2021.07.13 06/16 Sequelize + node.js express 사용연습 2 (0) 2021.06.16 06/16 Sequelize + node.js express 사용연습 (0) 2021.06.16 06/15 node.js서버 express CRUD 구현 (0) 2021.06.15 06/11 mysql 다운로드 , select, insert (0) 2021.06.11