SELECT, INSERT, UPDATE, DELETE
Node.js에서의 Mysql의 자세한 사용법은 github 페이지를 참고하세요.
SELECT
1
2
3
4
5
6
7
8
9
10
| var sql = 'SELECT * FROM table';
conn.query(sql, function(err, rows, fields){
if(err){
console.log(err);
} else {
for(var i=0; i<rows.length; i++){
console.log(rows[i].name);
}
}
});
|
INSERT
1
2
3
4
5
6
7
8
9
| var sql = 'INSERT INTO table (name, email, age) VALUES(?, ?, ?)';
var params = ['Jiwon', 'jiwon@mail.com', '23'];
conn.query(sql, params, function(err, rows, fields){
if(err){
console.log(err);
} else {
console.log(rows.insertId);
}
});
|
UPDATE
1
2
3
4
5
6
7
8
9
| var sql = 'UPDATE table SET name=?, age=? WHERE id=?';
var params = ['Eunji', '25', 1];
conn.query(sql, params, function(err, rows, fields){
if(err){
console.log(err);
} else {
console.log(rows);
}
});
|
DELETE
1
2
3
4
5
6
7
8
9
| var sql = 'DELETE FROM topic WHERE id=?';
var params = [1];
conn.query(sql, params, function(err, rows, fields){
if(err){
console.log(err);
} else {
console.log(rows);
}
});
|