[Node.js] 09 - Connect with Database

简介两个数据库:

 

Node.js 连接 MySql


导入已有数据库:

unsw@unsw-UX303UB$ mysql -u root -p #登录
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 44
Server version: 5.5.59-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> source /root/android-workplace/netty-server/db-mysql/localhost-testdb.sql #导入数据库 

基本操作:from link

1.显示数据库列表
show databases; 

2.选择一个数据库
use mysql   

3.显示当前数据库的表单:
show tables;

 

以上是常规操作,利用nodejs,该如何处理;

1. 安装nodejs环境下的mysql配置

(1) 
npm install mysql
(2) from link Be sure to run npm install
if you haven't tried that. If you have, try: npm install mysql --save

2. 数据库位置

mysql> select @@datadir;
+-----------------+
| @@datadir       |
+-----------------+
| /var/lib/mysql/ |
+-----------------+
1 row in set (0.00 sec)

root权限看到数据文件。

unsw@unsw-UX303UB$ sudo ls /var/lib/mysql/
[sudo] password for unsw: 
debian-5.5.flag  ib_logfile0  unsw_mh_test  mysql_upgrade_info
ibdata1          ib_logfile1  mysql         performance_schema

3. 使用nodejs操作数据库

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '123456',
port : '3306' database :
'mysql'  // 数据库的名字 }); connection.connect();

3.1 简单的select语句示范

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

3.2 查询数据

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('------------------------------------------------------------\n\n'); }); connection.end();

3.3 插入数据

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('-----------------------------------------------------------------\n\n');  
});
 
connection.end();

3.4 更新数据

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('-----------------------------------------------------------------\n\n');
});
 
connection.end();

3.5 删除数据

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('-----------------------------------------------------------------\n\n');  
});
 
connection.end();

 

 

 

Node.js 连接 MongoDB


 

MongoDB是一种文档导向数据库管理系统,基于分布式文件存储的数据库,由C++撰写而成。

本章节我们将为大家介绍如何使用 Node.js 来连接 MongoDB,并对数据库进行操作。

如果你还没有 MongoDB 的基本知识,可以参考我们的教程:MongoDB 教程

安装: 

Ref: How to Install and Configure MongoDB on Ubuntu 14.04

Ref: Linux平台安装MongoDB

 

posted @ 2018-04-08 19:20  郝壹贰叁  阅读(265)  评论(0编辑  收藏  举报