码迷,mamicode.com
首页 > 数据库 > 详细

nodejs MySQL操作

时间:2017-06-20 16:20:19      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:from   用户   div   base   local   word   安装   highlight   pre   

一  wamp创建数据库

选择phpMyAdmin

技术分享

 

选择用户,添加用户

技术分享

 

 填写数据库详细资料,填写完毕选择右下角的“执行”

 技术分享

 

用户添加成功

技术分享

 

2. nodejs 安装mysql驱动 

npm install mysql

技术分享

 

 3 数据库操作CURD

 

连接数据库

index.js:

var mysql      = require(‘mysql‘);
var connection = mysql.createConnection({
    host     : ‘localhost‘,
    user     : ‘test‘,
    password : ‘123456‘,
    database : ‘test‘
});

connection.connect();

connection.query(‘SELECT 1 + 1 AS solution‘, function (error, results, fields) {
    if (error) throw error;
    console.log(‘The solution is: ‘, results[0].solution);
});

 

运行后,输出结果:

技术分享

 

  查询

新建表account用于测试:

技术分享

 

index.js:

var mysql      = require(‘mysql‘);
var connection = mysql.createConnection({
    host     : ‘localhost‘,
    user     : ‘test‘,
    password : ‘123456‘,
    database : ‘test‘
});

connection.connect();

var  sql = ‘SELECT * FROM account‘;

connection.query(sql,function (err, result) {
    if(err){
        console.log(‘[SELECT ERROR] - ‘,err.message);
        return;
    }

    console.log(‘--------------------------SELECT----------------------------‘);
    console.log(result);
    console.log(result[0].id, result[0].name, result[0].age);
    console.log(‘------------------------------------------------------------\n\n‘);
});

connection.end();

  

输出结果:

技术分享

 

 插入数据

index.js:

var mysql      = require(‘mysql‘);
var connection = mysql.createConnection({
    host     : ‘localhost‘,
    user     : ‘test‘,
    password : ‘123456‘,
    database : ‘test‘
});

connection.connect();

var  addSql = ‘INSERT INTO account(id,name,age) VALUES(3,?,?)‘;  //插入数据
var  addSqlParams = [‘baby‘,23];                                 //填写问号的数据
//增
connection.query(addSql,addSqlParams,function (err, result) {
    if(err){
        console.log(‘[INSERT ERROR] - ‘,err.message);
        return;
    }

    console.log(‘--------------------------INSERT----------------------------‘);
    //console.log(‘INSERT ID:‘,result.insertId);
    console.log(‘INSERT ID:‘,result);   //插入结果
    console.log(‘-----------------------------------------------------------------\n\n‘);
});

connection.end();

 

运行结果,数据库增加了一条数据:

技术分享

 

技术分享

 

 更新数据

var mysql      = require(‘mysql‘);
var connection = mysql.createConnection({
    host     : ‘localhost‘,
    user     : ‘test‘,
    password : ‘123456‘,
    database : ‘test‘
});

connection.connect();

var modSql = ‘UPDATE account SET name = ?,age = ? WHERE id = ?‘;
var modSqlParams = [‘Lee‘, 31, 1];
//改
connection.query(modSql,modSqlParams,function (err, result) {
    if(err){
        console.log(‘[UPDATE ERROR] - ‘,err.message);
        return;
    }
    console.log(‘--------------------------UPDATE----------------------------‘);
    console.log(‘UPDATE affectedRows‘,result.affectedRows);
    console.log(‘-----------------------------------------------------------------\n\n‘);
});

connection.end();

 

运行结果:

技术分享

技术分享

 

  删除数据

var mysql      = require(‘mysql‘);
var connection = mysql.createConnection({
    host     : ‘localhost‘,
    user     : ‘test‘,
    password : ‘123456‘,
    database : ‘test‘
});

connection.connect();

var delSql = ‘DELETE FROM account where id=3‘;
//删
connection.query(delSql,function (err, result) {
    if(err){
        console.log(‘[DELETE ERROR] - ‘,err.message);
        return;
    }

    console.log(‘--------------------------DELETE----------------------------‘);
    console.log(‘DELETE affectedRows‘,result.affectedRows);
    console.log(‘-----------------------------------------------------------------\n\n‘);
});

connection.end();

 

运行结果,id=3的数据被删除:

技术分享

技术分享

 

  

 

nodejs MySQL操作

标签:from   用户   div   base   local   word   安装   highlight   pre   

原文地址:http://www.cnblogs.com/gamedaybyday/p/7054533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!