js查询mysql数据库数据_node.js操作mysql数据库之增删改查

安装mysql模块

npm install mysql

数据库准备

mysql server所在的机器IP地址是192.168.0.108,登录账户就用root@123456

在mysql中创建test数据库

在test数据库中创建一张users表

68bb3ad6376e594d36e935dc65eb43ed.png

操作

连接数据库

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) throwerr;

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) throwerr;

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) throwerr;

console.log('deleted zhangsan');

console.log(result);

console.log('\n');

});

查询user,所有

connection.query('select * from users', function(err, rows, fields) {if (err) throwerr;

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模块的完整功能说明参见官方:

全部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) throwerr;

console.log('inserted zhangsan');

console.log(result);

console.log('\n');

});

connection.query('select * from users', function(err, rows, fields) {if (err) throwerr;

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) throwerr;

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) throwerr;

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) throwerr;

console.log('deleted zhangsan');

console.log(result);

console.log('\n');

});

connection.query('select * from users', function(err, rows, fields) {if (err) throwerr;

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值