// MySQL 클라이언트 가져오기
const mysql = require('mysql2');
// 데이터베이스와 연결
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '123456q!',
database: 'mytest'
});
// insert - 반드시 placeholder와 함께 쓴다.
connection.query(
'insert into t_table set name=?, email=?',
['꺽정','kr@kr.kr'],
function(err) {
if (err)
throw err;
console.log('입력')
}
)
// 간단한 쿼리
connection.query(
'SELECT * FROM `t_table` ',
function(err, results, fields){
if (err)
throw err;
console.log(results); // results는 서버로부터 반환된 행들을 포함한다.
console.log(fields); // fields는 results에 관한 부가적인 메타데이터들을 포함한다.
}
);
// placeholder와 함께
connection.query(
'SELECT * FROM `t_table` WHERE `name` = ? AND `email` > ?',
['꺽정', 'kr@kr.kr'],
function(err, results){
if (err)
throw err;
console.log(results);
}
);