Home > Web Front-end > JS Tutorial > body text

Detailed introduction to the sample code sharing of nodejs operating mysql to implement addition, deletion, modification and query

黄舟
Release: 2017-05-28 10:33:39
Original
2111 people have browsed it

The following editor will bring you an example of nodejsoperationmysql to implement addition, deletion, modification and query. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

First you need toinstallmysql module: npm install mysql --save

Then create the user data table:

#Then use nodejs to add, delete, modify and check the database:

.【活动】2017 CSDN博客专栏评选   【评论送书】SQL优化、深度学习、数据科学家   CSDN日报20170527 ——《人机大战,历史的见证》   CSDN 日报 | 4.19-5.19 上榜作者排行出炉 
nodejs操作mysql实现增删改查 .
标签: nodejsjavascript实例数据库mysql 2017-05-19 18:39 98人阅读 评论(0) 收藏 举报 .本文章已收录于: 
 分类: javascript(31)  
作者同类文章XNodejs(2)  
作者同类文章X.版权声明:本文为博主原创文章,未经博主允许不得转载。
首先需要安装mysql模块:npm install mysql --save

然后创建user数据表:




接着使用nodejs对数据库进行增删改查:


//引入mysql模块
var mysql = require('mysql');
//链接数据库
var connection = mysql.createConnection({
  host:'localhost',
  user:'root',
  password:'root',
  database:'node',
})
connection.connect();
//查询数据
var sql = 'SELECT * FROM user';
connection.query(sql,function (err,result) {
  if(err){
    console.log('error');
    return;
  }
  console.log('-----------------查询----------------');
  console.log(result);
  console.log('-----------------查询结束----------------');
})

//增加数据
var addsql = 'INSERT INTO user(name,age,sex) VALUES(?,?,?)';
var addsqlparams = ['汪丹萍','24','女'];
connection.query(addsql,addsqlparams,function (err,result) {
  if(err){
    console.log('error');
    return;
  }
  console.log('-----------------新增成功----------------');
  console.log(result);
  console.log('-----------------结束----------------');
})

//修改数据
var modsql = 'UPDATE user SET name = ?,age = ? WHERE id = ?';
var modsqlparams = ['吕雪源love','26','1'];
connection.query(modsql,modsqlparams,function (err,result) {
  if(err){
    console.log('err');
    return;
  }
  console.log('--------------------------------');
  console.log(result);
  console.log('--------------------------------');
})

//删除数据
var delsql = 'DELETE FROM user where id = 2';
connection.query(delsql,function (err,result) {
  if(err){
    console.log('err');
    return;
  }
  console.log('----------删除-------------');
  console.log(result);
})


connection.end();
Copy after login

Finally run js:

#All additions, deletions, modifications and checks have been executed successfully.

The above is the detailed content of Detailed introduction to the sample code sharing of nodejs operating mysql to implement addition, deletion, modification and query. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!