首頁 > web前端 > js教程 > Node.js操作mysql資料庫增刪改查_node.js

Node.js操作mysql資料庫增刪改查_node.js

WBOY
發布: 2016-05-16 15:07:29
原創
3266 人瀏覽過

關於node.js操作mysql資料庫的相關介紹請閱讀全文吧。下文介紹的非常詳細,具體內容如下:

 安裝mysql模組

npm install mysql 
登入後複製

資料庫準備

mysql server所在的機器IP位址是192.168.0.108,登入帳號就用root@123456

在mysql建立test資料庫

在test資料庫中建立一張users表

操作

連接資料庫

var mysql=require('mysql');
var connection = mysql.createConnection({
host : '192.168.0.108',
user : 'root',
password : '123456',
database : 'test1',
port:'3306'
});
connection.connect();
登入後複製

插入一個user

var usr={name:'zhangsan',password:'pwdzhangsan',mail:'zhangsan@gmail.com'};
connection.query('insert into users set ?', usr, function(err, result) {
if (err) throw err;
console.log('inserted zhangsan');
console.log(result);
console.log('\n');
});
登入後複製

更新user,帶條件

connection.query('update users set password="ddd" where name="zhangsan"', {password:'ppp'}, function(err, result) {
if (err) throw err;
console.log('updated zhangsan\'s password to ddd');
console.log(result);
console.log('\n');
}); 
登入後複製

刪除一個user,帶條件

connection.query('delete from users where name="zhangsan"', {password:'ppp'}, function(err, result) {
if (err) throw err;
console.log('deleted zhangsan');
console.log(result);
console.log('\n');
});
登入後複製

查詢user,所有

connection.query('select * from users', function(err, rows, fields) {
if (err) throw err;
console.log('selected after deleted');
for(var i= 0,usr;usr=rows[i++];){
console.log('user nae='+usr.name + ', password='+usr.password);
}
console.log('\n');
}); 
登入後複製

關閉資料庫連線

connection.end();

基本的CRUD完成

mysql模組的完整功能說明請參閱官方:

https://www.npmjs.com/package/mysql

https://github.com/felixge/node-mysql

全部demo程式碼

var mysql=require('mysql');
var connection = mysql.createConnection({
host : '192.168.0.108',
user : 'root',
password : '123456',
database : 'test1',
port:'3306'
});
connection.connect();
var usr={name:'zhangsan',password:'pwdzhangsan',mail:'zhangsan@gmail.com'};
connection.query('insert into users set ?', usr, function(err, result) {
if (err) throw err;
console.log('inserted zhangsan');
console.log(result);
console.log('\n');
});
connection.query('select * from users', function(err, rows, fields) {
if (err) throw err;
console.log('selected after inserted');
for(var i= 0,usr;usr=rows[i++];){
console.log('user nae='+usr.name + ', password='+usr.password);
}
console.log('\n');
});
connection.query('update users set password="ddd" where name="zhangsan"', {password:'ppp'}, function(err, result) {
if (err) throw err;
console.log('updated zhangsan\'s password to ddd');
console.log(result);
console.log('\n');
});
connection.query('select * from users', function(err, rows, fields) {
if (err) throw err;
console.log('selected after updated');
for(var i= 0,usr;usr=rows[i++];){
console.log('user nae='+usr.name + ', password='+usr.password);
}
console.log('\n');
});
connection.query('delete from users where name="zhangsan"', {password:'ppp'}, function(err, result) {
if (err) throw err;
console.log('deleted zhangsan');
console.log(result);
console.log('\n');
});
connection.query('select * from users', function(err, rows, fields) {
if (err) throw err;
console.log('selected after deleted');
for(var i= 0,usr;usr=rows[i++];){
console.log('user nae='+usr.name + ', password='+usr.password);
}
console.log('\n');
});
connection.end();
登入後複製

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板