Node实战篇:Nodejs 链接 Mariadb 实例

图片

没有数据库的后台可能只是前端+网络而已,之前对Mariadb系统的学习一段时间,加之Nodejs以及express的使用,现将用一个小的实例将nodejs与关系型数据库---MariaDB关联起来,当然非关系型数据库---Mongodb之后会在一个稍微大点的项目中依次罗列出来.

该文章的主要目标在于: Nodejs与MaiarDB关联;

第一部分 配置 Mariadb

1.1 Mariadb 概述

对于Mariadb的历史与渊源在此不过多累述,可以理解为开源的/升级的/MySQL的孪生兄弟.
如需详细文档请点击Mariadb系列文章, 在此简要将配置罗列一下(以Mac为例子)

1. xcode-select --install 下载最新xcode
2. 配置并检测Homebrew; 3. 下载MariaDB: brew install mariadb
4. 启动数据库服务: mysql.server start5. 连接数据库: mysql -u root -p
1.2 启动数据服务,链接数据库
BWF-huanghaowei:~ 51Code$ mysql.server start 
Starting MySQL
 SUCCESS! 
 
BWF-huanghaowei:~ 51Code$ mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3Server version: 10.1.19-MariaDB Homebrew

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

第二部分 Nodejs相关配置

2.1 Nodejs相关配置

MariaDB官网提供关联库---mariasqlgithub地址

2.2 实例代码
2.2.1 下载 mariasql
Desktop $ mkdir Test
$ cd Test#下载配置, 中间可能会提示很多信息,在此忽略$ npm install mariasql
...
..
.
4 warnings generated.  SOLINK_MODULE(target) Release/sqlclient.nodeclang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9/Users/51testing/Desktop/Test/asf └─┬ mariasql@0.2.6  ├── lru-cache@2.7.3  └── nan@2.5.0 $ ls node_modules
2.2.2 编写index.js文件

[默认之前已经启动Mariadb服务]

测试数据与表格
//表格
CREATE TABLE `orders` (  
   `order_id` int(11) NOT NULL AUTO_INCREMENT,  
   `order_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  
   `order_amount` float(6,2) DEFAULT NULL,  
   `customer_id` int(11) DEFAULT NULL,  PRIMARY KEY (`order_id`) );
//插入数据
INSERT INTO `orders` VALUES (1,'2016-12-14 04:02:35',111.00,1),(2,'2016-11-11 14:22:22',222222.00,2),(3,'2016-11-11 15:33:33',1.00,1),(4,'2016-12-12 03:11:11',123456.00,3),(5,'2016-12-14 07:18:17',1234.00,5);
nodejs代码
//链接
Mariasql
var Client = require('mariasql');
//配置相关信息
var c = new Client({  
 host: '127.0.0.1',  //用户名  user: 'root',  //密码默认为空  password: '',  //使用哪个数据库  db: 'user_db'}); c.query('SHOW DATABASES', function(err, rows) {  
   if (err)    
       throw err;    
       console.log('---------查看所有的数据库------------');    
       console.dir(rows); });
//使用array的形式快于对象,效果一样
c.query('SHOW TABLES', null,
   { useArray: true },
   function(err, rows) {  
       if (err)    
       throw err;  console.log('--------查看所有的数据表格-------------');  console.dir(rows); });
//结合使用SQL语句
c.query('SELECT * FROM orders', function(err, rows) {  
   if (err)    throw err;  
   console.log('--------查看orders的数据-------------');  
   console.dir(rows); });
//使用占位符
c.query('SELECT * FROM orders WHERE order_id = ? AND customer_id = ?',        [ 5, 5 ],        
   function(err, rows) {  
       if (err)    throw err;  
       console.log('--------SELECT + WHERE-------------');    console.dir(rows); });
//定义sql语句, 稍后使用,另外一种占位符
var prep = c.prepare('SELECT * FROM orders WHERE order_id = :orderid AND customer_id = :customerid'); c.query(prep({orderid: 5, customerid: 5}),
   function(err, rows){    
       if (err) {        
       throw err    } else {        
       console.log('--------SELECT + WHERE-------------');            console.dir(rows);    } })
//关闭数据库
c.end();

打开终端,切换至该目录下, 执行$ node index即可.

更多设置请前往mariasql

在此只是简单介绍基本链接与使用,如果有兴趣可多参阅一些SQL注入相关知识与内容


猜你喜欢

转载自blog.51cto.com/15127576/2668213