沒有數(shù)據(jù)庫的后臺可能只是前端+網(wǎng)絡而已,之前對Mariadb系統(tǒng)的學習一段時間,加之Nodejs以及express的使用,現(xiàn)將用一個小的實例將nodejs與關(guān)系型數(shù)據(jù)庫---MariaDB關(guān)聯(lián)起來,當然非關(guān)系型數(shù)據(jù)庫---Mongodb之后會在一個稍微大點的項目中依次羅列出來.
該文章的主要目標在于: Nodejs與MaiarDB關(guān)聯(lián);
第一部分 配置 Mariadb
1.1 Mariadb 概述
對于Mariadb的歷史與淵源在此不過多累述,可以理解為開源的/升級的/MySQL的孿生兄弟.
如需詳細文檔請點擊Mariadb系列文章, 在此簡要將配置羅列一下(以Mac為例子)
1. xcode-select --install 下載最新xcode
2. 配置并檢測Homebrew;
3. 下載MariaDB: brew install mariadb
4. 啟動數(shù)據(jù)庫服務: mysql.server start
5. 連接數(shù)據(jù)庫: mysql -u root -p
1.2 啟動數(shù)據(jù)服務,鏈接數(shù)據(jù)庫
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 3
Server 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相關(guān)配置
2.1 Nodejs相關(guān)配置
MariaDB官網(wǎng)提供關(guān)聯(lián)庫---mariasqlgithub地址
2.2 實例代碼
2.2.1 下載 mariasql
Desktop $ mkdir Test
$ cd Test
#下載配置, 中間可能會提示很多信息,在此忽略
$ npm install mariasql
...
..
.
4 warnings generated.
SOLINK_MODULE(target) Release/sqlclient.node
clang: 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文件
[默認之前已經(jīng)啟動Mariadb服務]
測試數(shù)據(jù)與表格
//表格
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`)
);
//插入數(shù)據(jù)
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');
//配置相關(guān)信息
var c = new Client({
host: '127.0.0.1',
//用戶名
user: 'root',
//密碼默認為空
password: '',
//使用哪個數(shù)據(jù)庫
db: 'user_db'
});
c.query('SHOW DATABASES', function(err, rows) {
if (err)
throw err;
console.log('---------查看所有的數(shù)據(jù)庫------------');
console.dir(rows);
});
//使用array的形式快于對象,效果一樣
c.query('SHOW TABLES', null, { useArray: true }, function(err, rows) {
if (err)
throw err;
console.log('--------查看所有的數(shù)據(jù)表格-------------');
console.dir(rows);
});
//結(jié)合使用SQL語句
c.query('SELECT * FROM orders', function(err, rows) {
if (err)
throw err;
console.log('--------查看orders的數(shù)據(jù)-------------');
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);
}
})
//關(guān)閉數(shù)據(jù)庫
c.end();
打開終端,切換至該目錄下, 執(zhí)行$ node index即可.
在此只是簡單介紹基本鏈接與使用,如果有興趣可多參閱一些SQL注入相關(guān)知識與內(nèi)容
更多精彩內(nèi)容請關(guān)注“IT實戰(zhàn)聯(lián)盟”哦~~~

IT實戰(zhàn)聯(lián)盟.jpg