node連接mysql,并操作mysql方式
node連接mysql
一.了解mysql常用語句
1.打開mysql
mysql -u root -p(你已經(jīng)配置好mysql)
輸入設(shè)置的密碼,當(dāng)出現(xiàn)mysql> 就代表開啟成功
(需要在安裝目錄下的bin目錄下打開,最好用管理員權(quán)限打開)

2.創(chuàng)建數(shù)據(jù)庫
- 語法:create database datashow;
- 使用:use datashshow;
- 展示所有的數(shù)據(jù)庫:show databases;

3.創(chuàng)建數(shù)據(jù)表
- 語法:create table show(表名)(
- 字段名1 數(shù)據(jù)類型 【屬性】【索引】,
- 字段名1 數(shù)據(jù)類型 【屬性】【索引】,
- ---------
- )【存儲引擎】【表字符集】;

4.插入數(shù)據(jù)
- 語法:INSERT 表名【(字段名,字段名,字段名........)】
- VALUE (值1,值2,.....值n)
- 查詢:select * from 表名

5.修改表數(shù)據(jù)
- update 表名 set 字段名1=表達(dá)式1,【字段名2=表達(dá)式2,......,字段名n=表達(dá)式n】
二.連接mysql
- 1.引入mysql模塊,(mysql庫)
- 2.創(chuàng)建數(shù)據(jù)庫連接
- 3.斷開數(shù)據(jù)庫連接
| 屬性 | 含義 |
| host | 連接數(shù)據(jù)庫服務(wù)器名(默認(rèn)為localhost) |
| port | 連接端口號 |
| user | mysql服務(wù)器連接用戶名 |
| password | mysql服務(wù)器連接密碼 |
| database | 要連接的數(shù)據(jù)庫 |
| charset | 連接使用的字符編碼 |
| timezone | 連接使用的時區(qū) |
//需要在文件夾下載mysql模塊
//npm install mysql
const mysql=require('mysql')
var coonnection=mysql.createConnection({
host:'localhost',
user:'root',
password:'123456',
database:'datashow'
})
coonnection.connect(function(err){
if(err){
console.log("connect is not good"+err.stack)
return ;
}
console.log("connect is ok"+coonnection.threadId)
})//斷開數(shù)據(jù)庫連接 connection.end()
我將上面的代碼放在connection.js中

三.操作數(shù)據(jù)庫
1.查詢數(shù)據(jù)
創(chuàng)建select.js用于查詢數(shù)據(jù)庫中的數(shù)據(jù)。
主要用到的語法就時 select * from student
const mysql=require('mysql')
var coonnection=mysql.createConnection({
host:'localhost',
user:'root',
password:'123456',
database:'datashow'
})
coonnection.connect(function(err){
if(err){
console.log("connect is not good"+err.stack)
return ;
}
console.log("connect is ok: "+coonnection.threadId)
})
var sql='select * from student'
coonnection.query(sql,(err,result)=>{
if(err) throw err
console.log(result)
})
2.修改數(shù)據(jù)
創(chuàng)建changeDate.js用于對數(shù)據(jù)庫數(shù)據(jù)修改。
用到的語法就是
- 插入:insert into 表名 values(.....)
- 更新:update 表名 set 字段1=表達(dá)式1 ,......字段n=表達(dá)式n
- 刪除:delete from 表名 where 字段1=?(需要刪除的數(shù)據(jù))
const mysql=require('mysql')
var coonnection=mysql.createConnection({
host:'localhost',
user:'root',
password:'123456',
database:'datashow'
})
coonnection.connect(function(err){
console.log("connect is ok: "+coonnection.threadId)
})
//用于向數(shù)據(jù)庫添加數(shù)據(jù)
var addSql='INSERT INTO student VALUES(?,?,?,?,?)'
var addSqlDate=['116','糖糖','女','1999-2-3','2003']
coonnection.query(addSql,addSqlDate,(err,result)=>{
if(err) throw err
console.log('添加成功')
})
//用于向數(shù)據(jù)庫更新指定的數(shù)據(jù)
var updatesql='UPDATE student SET sName=?,sSex=? WHERE sNo=?'
var updatesqlDate=['青青','女',103]
coonnection.query(updatesql,updatesqlDate,(err,result)=>{
if(err) throw err
console.log('修改成功')
})
//用于向數(shù)據(jù)庫刪除指定的數(shù)據(jù)
var deletesql='DELETE FROM student where sNo=?'
var deletesqlDate=['104']
coonnection.query(deletesql,deletesqlDate,(err,result)=>{
if(err) throw err
console.log('刪除成功')
})總結(jié)
在書寫mysql代碼時一定要在結(jié)尾處加上;
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Node.js實(shí)現(xiàn)Markdown文檔在線編輯功能
這篇文章主要介紹了如何基于 Node.js 和 mermaid.js 的 Web 服務(wù)器程序,實(shí)現(xiàn)在線編輯 Markdown 文檔并支持 mermaid.js 流程圖渲染的功能,感興趣的小伙伴可以了解下2026-02-02
如何用npm命令刪除開發(fā)項(xiàng)目中的node_modules文件夾
每個項(xiàng)目都會產(chǎn)生一個node_modules,每個node_modules少則幾十兆,多則幾百甚至上千兆,隨著時間的積累,維護(hù)項(xiàng)目的增加,整個項(xiàng)目目錄體積會越來越大,這篇文章主要給大家介紹了關(guān)于如何用npm命令刪除開發(fā)項(xiàng)目中的node_modules文件夾,需要的朋友可以參考下2023-12-12
node.js利用socket.io實(shí)現(xiàn)多人在線匹配聯(lián)機(jī)五子棋
這篇文章主要介紹了node.js利用socket.io實(shí)現(xiàn)多人在線匹配聯(lián)機(jī)五子棋的操作方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-05-05
npm 更改默認(rèn)全局路徑以及國內(nèi)鏡像的方法
今天小編就為大家分享一篇npm 更改默認(rèn)全局路徑以及國內(nèi)鏡像的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
node實(shí)現(xiàn)定時發(fā)送郵件的示例代碼
本篇文章主要介紹了node實(shí)現(xiàn)定時發(fā)送郵件的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

