欢迎来到代码驿站!

当前位置:首页 >

Node.js操作mysql数据库增删改查

时间:2023-03-01 15:11:48|栏目:|点击:

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

上一篇:Perl批量下载Gmail附件的代码

栏    目:

下一篇:android Retrofit2+okHttp3使用总结

本文标题:Node.js操作mysql数据库增删改查

本文地址:http://www.codeinn.net/misctech/226706.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有