Node.js操作mysql数据库增删改查_node.js
程序员文章站
2022-03-11 15:49:25
...
关于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();
上一篇: mysql数据库难学吗?
下一篇: 数据库中存储过程和触发器有什么区别
推荐阅读
-
node.js使用mongoose操作数据库实现购物车的增、删、改、查功能示例
-
Node.js数据库操作之查询MySQL数据库(二)
-
从零学习node.js之mysql数据库的操作(五)
-
Node.js数据库操作之查询MySQL数据库(二)
-
Python操作MySQL数据库实例详解【安装、连接、增删改查等】
-
Node.js数据库操作之连接MySQL数据库(一)
-
python数据库-MySQL数据库的增删改查基本操作(49)
-
mysql数据库表的创建以及字段的增删改查操作及一些常用的查询命令介绍
-
从零学习node.js之mysql数据库的操作(五)
-
Python实现连接MySql数据库及增删改查操作详解