首页 文章 个人博客 基于NodeJs操作MySQL实现基础操作

基于NodeJs操作MySQL实现基础操作

发布时间:2021-04-11编辑:RainNight阅读(75)

基于NodeJs操作MySQL实现基础操作


实现数据库的增删改查


代码篇

const mysql = require('mysql');

const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '1234567890',
    database: 'Demo'
});

connection.connect();

var addSql = 'INSERT INTO profile(student) VALUES(?)';
var addSqlParams = ['菜鸟工具2212121'];
//增
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');
});

var modSql = 'UPDATE profile SET student = ? WHERE Id = ?';
var modSqlParams = ['菜鸟移动站', 7];
//改
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');
});

var delSql = 'DELETE FROM profile where Id = ?';
var delSqlParams = [10];
//删
connection.query(delSql, delSqlParams, 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');
});



const sql = "SELECT * from profile";
//查
connection.query(sql, function (err, result) {
    if (err) {
        console.log('[SELECT ERROR] - ', err.message);
        return;
    }

    console.log('--------------------------SELECT(查)----------------------------');
    console.log(result);
    console.log('------------------------------------------------------------\n\n');
});

connection.end();

结果:

$ node MySQL.js
--------------------------INSERT()----------------------------
INSERT ID: OkPacket {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 13,
  serverStatus: 2,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0
}
-----------------------------------------------------------------


--------------------------UPDATE()----------------------------
UPDATE affectedRows 1
-----------------------------------------------------------------


--------------------------DELETE(删除)----------------------------
DELETE affectedRows 1
-----------------------------------------------------------------


--------------------------SELECT----------------------------
[
  RowDataPacket { id: 1, student: 'Abbot' },
  RowDataPacket { id: 2, student: 'Doris' },
  RowDataPacket { id: 3, student: 'Emerson' },
  RowDataPacket { id: 4, student: 'Green' },
  RowDataPacket { id: 5, student: 'Jeames' },
  RowDataPacket { id: 6, student: '菜鸟工具' },
  RowDataPacket { id: 7, student: '菜鸟移动站' },
  RowDataPacket { id: 11, student: '菜鸟工具2212121' },
  RowDataPacket { id: 12, student: '菜鸟工具2212121' },
  RowDataPacket { id: 13, student: '菜鸟工具2212121' }
]

网盘推荐

微信公众号