node.js - 2
2019.10.08 08:32

Node.js MySQL Select From

조회 수 630 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

테이블 선택

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  con.query("SELECT * FROM customers", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
});

 

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

 

결과는 아래와 같다.

[
  { id: 1, name: 'John', address: 'Highway 71'},
  { id: 2, name: 'Peter', address: 'Lowstreet 4'},
  { id: 3, name: 'Amy', address: 'Apple st 652'},
  { id: 4, name: 'Hannah', address: 'Mountain 21'},
  { id: 5, name: 'Michael', address: 'Valley 345'},
  { id: 6, name: 'Sandy', address: 'Ocean blvd 2'},
  { id: 7, name: 'Betty', address: 'Green Grass 1'},
  { id: 8, name: 'Richard', address: 'Sky st 331'},
  { id: 9, name: 'Susan', address: 'One way 98'},
  { id: 10, name: 'Vicky', address: 'Yellow Garden 2'},
  { id: 11, name: 'Ben', address: 'Park Lane 38'},
  { id: 12, name: 'William', address: 'Central st 954'},
  { id: 13, name: 'Chuck', address: 'Main Road 989'},
  { id: 14, name: 'Viola', address: 'Sideway 1633'}
]

 

컬럼 선택

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  con.query("SELECT name, address FROM customers", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
});

 

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

 

결과는 아래와 같다.

[
  { name: 'John', address: 'Highway 71'},
  { name: 'Peter', address: 'Lowstreet 4'},
  { name: 'Amy', address: 'Apple st 652'},
  { name: 'Hannah', address: 'Mountain 21'},
  { name: 'Michael', address: 'Valley 345'},
  { name: 'Sandy', address: 'Ocean blvd 2'},
  { name: 'Betty', address: 'Green Grass 1'},
  { name: 'Richard', address: 'Sky st 331'},
  { name: 'Susan', address: 'One way 98'},
  { name: 'Vicky', address: 'Yellow Garden 2'},
  { name: 'Ben', address: 'Park Lane 38'},
  { name: 'William', address: 'Central st 954'},
  { name: 'Chuck', address: 'Main Road 989'},
  { name: 'Viola', address: 'Sideway 1633'}
]

 

결과 처리

console.log(result[2].address);

 

Apple st 652

 

필드 객체

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  con.query("SELECT name, address FROM customers", function (err, result, fields) {
    if (err) throw err;
    console.log(fields);
  });
});

 

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

 

출력 결과는 아래와 같다.

[
  {
    catalog: 'def',
    db: 'mydb',
    table: 'customers',
    orgTable: 'customers',
    name: 'name',
    orgName: 'address',
    charsetNr: 33,
    length: 765,
    type: 253,
    flags: 0,
    decimals: 0,
    default: undefined,
    zeroFill: false,
    protocol41: true
  },
  {
    catalog: 'def',
    db: 'mydb',
    table: 'customers',
    orgTable: 'customers',
    name: 'address',
    orgName: 'address',
    charsetNr: 33,
    length: 765,
    type: 253,
    flags: 0,
    decimals: 0,
    default: undefined,
    zeroFill: false,
    protocol41: true
  {
]

 

console.log(fields[1].name);

 

address

 

 

?

List of Articles
번호 분류 제목 날짜 조회 수
38 node.js - 1 Express에서 POST 방식 사용하기 2019.10.16 349
37 node.js - 1 Express에서 MySql 사용 2019.10.10 843
36 node.js - 2 Node.js MySQL Limit 2019.10.10 288
35 node.js - 2 Node.js MySQL Update 2019.10.10 276
34 node.js - 2 Node.js MySQL Drop Table 2019.10.08 357
33 node.js - 2 Node.js MySQL Delete 2019.10.08 302
32 node.js - 2 Node.js MySQL Order By 2019.10.08 285
31 node.js - 2 Node.js MySQL Where 2019.10.08 494
» node.js - 2 Node.js MySQL Select From 2019.10.08 630
29 node.js - 2 Node.js MySQL Insert Into 2019.10.01 933
28 node.js - 2 Node.js MySQL Create Table 2019.10.01 324
27 PUG PUG 07 / INCLUDES 2019.10.01 358
26 PUG PUG 06 / DOCTYPE 2019.10.01 431
25 node.js - 2 Node.js MySQL 2019.09.27 315
24 node.js - 2 NPM의 사용 2019.09.26 313
23 node.js - 2 Node.js URL Module 2019.09.26 461
22 PUG PUG 05 / Conditionals 2019.09.19 755
21 PUG PUG 04 / Comments 2019.09.19 329
20 PUG PUG 03 / Code 2019.09.19 691
19 PUG PUG 02 / Case 2019.09.19 367
Board Pagination Prev 1 2 Next
/ 2