nodejs連接數(shù)據(jù)庫
經(jīng)過了前面的學習抗楔,相信你一定了解了nodejs是一個包依賴的開發(fā)方式棋凳,用哪個功能就require哪個模塊,萬一這個模塊自身沒有谓谦,就通過npm安裝一個贫橙。
使用數(shù)據(jù)庫也是一樣的,如果你需要使用mysql來儲存數(shù)據(jù)反粥,就安裝一個mysql模塊卢肃。
準備
首先需要在項目中安裝mysql
npm install mysql
然后引用mysql模塊,在mysql模塊中才顿,可以使用createConnection方法來創(chuàng)建一個表示與mysql服務(wù)器之間連接的Connection對象莫湘。
//配置數(shù)據(jù)庫對象
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '數(shù)據(jù)庫地址如:localhost',
user: '數(shù)據(jù)庫用戶名',
password: '密碼',
database:'數(shù)據(jù)庫名'
});
connection.connect(); //連接數(shù)據(jù)庫
//執(zhí)行操作
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
//關(guān)閉連接
connection.end();
createConnection 方法中可以傳入一個json對象,里面的內(nèi)容不止這些郑气。
然后幅垮,執(zhí)行connect方法,與mysql數(shù)據(jù)庫進行連接尾组。
然后忙芒,調(diào)用connection對象的query方法來統(tǒng)一執(zhí)行數(shù)據(jù)的增刪改查示弓。
常用方法
mysql常用增刪該查語句:
增
1.1【插入單行】
insert [into] <表名> (列名) values (列值)
例:
insert into Strdents (姓名,性別,出生日期) values ('開心朋朋','男','1980/6/15')
1.2【將現(xiàn)有表數(shù)據(jù)添加到一個已有表】
insert into <已有的新表> (列名) select <原表列名> from <原表名>
例:
insert into tongxunlu ('姓名','地址','電子郵件')
select name,address,email
from Strdents
1.3【直接拿現(xiàn)有表數(shù)據(jù)創(chuàng)建一個新表并填充】
select <新建表列名> into <新建表名> from <源表名>
例:
select name,address,email into tongxunlu from strdents
1.4【使用union關(guān)鍵字合并數(shù)據(jù)進行插入多行】
insert <表名> <列名> select <列值> tnion select <列值>
例:
insert Students (姓名,性別,出生日期)
select '開心朋朋','男','1980/6/15' union(union表示下一行)
select '藍色小明','男','19**/**/**'
刪
2.1【刪除<滿足條件的>行】
delete from <表名> [where <刪除條件>]
例:
delete from a where name='開心朋朋'(刪除表a中列值為開心朋朋的行)
2.2【刪除整個表】
truncate table <表名>
truncate table tongxunlu
注意:刪除表的所有行,但表的結(jié)構(gòu)呵萨、列奏属、約束、索引等不會被刪除潮峦;不能用語有外建約束引用的表
改
update <表名> set <列名=更新值> [where <更新條件>]
例:
update tongxunlu set 年齡=18 where 姓名='藍色小名'
查
4.1精確(條件)查詢
select <列名> from <表名> [where <查詢條件表達試>] [order by <排序的列名>[asc或desc]]
4.1.1【查詢所有數(shù)據(jù)行和列】
例:
select * from a
說明:查詢a表中所有行和列
4.1.2【查詢部分行列--條件查詢】
例:
select i,j,k from a where f=5
說明:查詢表a中f=5的所有行囱皿,并顯示i,j,k3列
4.1.3【在查詢中使用AS更改列名】
例:
select name as 姓名 from a where xingbie='男'
說明:查詢a表中性別為男的所有行,顯示name列忱嘹,并將name列改名為(姓名)顯示
4.1.4【查詢空行】
例:
select name from a where email is null
說明:查詢表a中email為空的所有行嘱腥,并顯示name列;SQL語句中用is null或者is not null來判斷是否為空行
4.1.5【在查詢中使用常量】
例:
select name, '唐山' as 地址 from Student
說明:查詢表a拘悦,顯示name列齿兔,并添加地址列,其列值都為'唐山'
4.1.6【查詢返回限制行數(shù)(關(guān)鍵字:top percent)】
例1:
select top 6 name from a
說明:查詢表a窄做,顯示列name的前6行愧驱,top為關(guān)鍵字
例2:
select top 60 percent name from a
說明:查詢表a,顯示列name的60%椭盏,percent為關(guān)鍵字
4.1.7【查詢排序(關(guān)鍵字:order by , asc , desc)】
例:
select name
from a
where chengji>=60
order by desc
說明:查詢a表中chengji大于等于60的所有行组砚,并按降序顯示name列;默認為ASC升序
4.2模糊查詢
4.2.1【使用like進行模糊查詢】
注意:like運算副只用于字符串掏颊,所以僅與char和varchar數(shù)據(jù)類型聯(lián)合使用
例:
select * from a where name like '趙%'
說明:查詢顯示表a中糟红,name字段第一個字為趙的記錄
4.2.2【使用between在某個范圍內(nèi)進行查詢】
例:
select * from a where nianling between 18 and 20
說明:查詢顯示表a中nianling在18到20之間的記錄
4.2.3【使用in在列舉值內(nèi)進行查詢】
例:
select name from a where address in ('北京','上海','唐山')
說明:查詢表a中address值為北京或者上海或者唐山的記錄乌叶,顯示name字段
4.3 分組查詢
4.3.1【使用group by進行分組查詢】
例:
select studentID as 學員編號,AVG(score) as 平均成績 (注釋:這里的score是列名)
from score (注釋:這里的score是表名)
group by studentID
說明:在表score中查詢盆偿,按strdentID字段分組,顯示strdentID字段和score字段的平均值准浴;select語句中只允許被分組的列和為每個分組返回的一個值的表達式事扭,例如用一個列名作為參數(shù)的聚合函數(shù)
4.3.2【使用having子句進行分組篩選】
例:
select studentID as 學員編號,AVG(score) as 平均成績 (注釋:這里的score是列名)
from score (注釋:這里的score是表名)
group by studentID
having count(score)>1
說明:接上面例子,顯示分組后count(score)>1的行乐横,由于where只能在沒有分組時使用求橄,分組后只能使用having來限制條件。
4.4 多表聯(lián)接查詢
4.4.1內(nèi)聯(lián)接
4.4.1.1【在where子句中指定聯(lián)接條件】
例:
select a.name,b.chengji
from a,b
where a.name=b.name
說明:查詢表a和表b中name字段相等的記錄葡公,并顯示表a中的name字段和表b中的chengji字段
4.4.1.2【在from子句中使用join…on】
例:
select a.name,b.chengji
from a inner join b
on (a.name=b.name)
說明:同上
4.4.2 外聯(lián)接
4.4.2.1【左外聯(lián)接查詢】
例:
select s.name,c.courseID,c.score
from strdents as s
left outer join score as c
on s.scode=c.strdentID
說明:在strdents表和score表中查詢滿足on條件的行罐农,條件為score表的strdentID與strdents表中的sconde相同
4.4.2.2【右外聯(lián)接查詢】
例:
select s.name,c.courseID,c.score
from strdents as s
right outer join score as c
on s.scode=c.strdentID
說明:在strdents表和score表中查詢滿足on條件的行,條件為strdents表中的sconde與score表的strdentID相同