• Node.js 数据库


    通过Node.js语言连接数据库,通过Express框架指明数据库连接的位置。

    一、Express 框架下Node.js连接数据库的写法

    在同一个目录下写setting.js和connet.js,分别负责数据库的设置和连接

    setting.js

    module.exports = {
    host:'……',
    user:'root',
    password:'123456',
    port:3306,
    database:'数据库名称'
    };

    connect.js

    var settings = require('./settings');
    var mysql = require('mysql');
    
    var client = mysql.createConnection(settings, function(err, stream) {
    });
    
    client.connect(function(err) {
    if (err) {
    console.log('error connecting: ' + err.stack);
    throw err;
    }
    console.log('connect as id' + client.threadId);
    })
    client.on('error', function() {});
    module.exports = client;

    express框架下数据库模块的加载

    let db = require('./database/querydb.js');
    
    app.use(express.static('./public'));
    app.use(express.static('./database'));
    app.use(express.static('./build'))

    二、单纯的Node.js数据库连接与操作

    数据库连接:

    var mysql = require('mysql'); 
    var connection = mysql.createConnection({ 
    host : 'localhost', 
    user : 'root', 
    password : '123456', 
    port: '3306', 
    database: 'transport', 
    });
    
    connection.connect(function(err) {
    if (err) {
    console.log('error connecting: ' + err.stack);
    throw err;
    }
    console.log('connect as id' + client.threadId);
    })
    View Code

    select操作

    var sql = 'SELECT * FROM websites';
    //
    connection.query(sql,function (err, result) {
    if(err){
    console.log('[SELECT ERROR] - ',err.message);
    return;
    }
    console.log('--------------------------SELECT----------------------------');
    console.log(result);
    console.log('------------------------------------------------------------
    
    '); 
    });
    connection.end();
    View Code

    delete操作

    var delSql = 'DELETE FROM websites where id=6';
    //
    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('-----------------------------------------------------------------
    
    '); 
    });
    
    connection.end();
    View Code

    insert操作

    var mysql = require('mysql');
    
    var connection = mysql.createConnection({ 
    host : 'localhost', 
    user : 'root', 
    password : '123456', 
    port: '3306', 
    database: 'test', 
    }); 
    connection.connect();
    
    var addSql = 'INSERT INTO websites(Id,name,url,alexa,country) VALUES(0,?,?,?,?)';
    var addSqlParams = ['菜鸟工具', 'https://c.runoob.com','23453', 'CN'];
    //
    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('-----------------------------------------------------------------
    
    '); 
    });
    
    connection.end();
    View Code

    update操作

    var modSql = 'UPDATE websites SET name = ?,url = ? WHERE Id = ?';
    var modSqlParams = ['菜鸟移动站', 'https://m.runoob.com',6];
    //
    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('-----------------------------------------------------------------
    
    ');
    });
    connection.end();
    View Code
  • 相关阅读:
    python装饰器的wraps作用
    lambda函数和map函数
    python直接赋值、切片、浅拷贝和深拷贝
    ubuntu shell脚本出错 dash
    关于方法论和相关书籍
    如何安全的大数据量表在线进行DML操作
    mysql group by 查询非聚集列
    MongoTemplate进行增删改查
    Mockito 的用法
    一个人开始优秀的3种迹象
  • 原文地址:https://www.cnblogs.com/starryxsky/p/7357343.html
Copyright © 2020-2023  润新知