欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Node.js中操作MySQL數(shù)據(jù)庫的方法

這篇文章給大家分享的是有關(guān)Node.js中操作MySQL數(shù)據(jù)庫的方法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯(lián)公司服務(wù)項目包括沈北新網(wǎng)站建設(shè)、沈北新網(wǎng)站制作、沈北新網(wǎng)頁制作以及沈北新網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,沈北新網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到沈北新省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

使用mysql這個npm模塊操作MySQL數(shù)據(jù)庫的基礎(chǔ)教程。 不涉及MySQL的安裝和配置,如果電腦中還未安裝MySQL, 推薦安裝WAMP、XAMPP等集成環(huán)境。本文中還使用到了輕量級的Node.js框架Koa搭建web程序,為的是通過前端瀏覽器請求的方式來模擬項目場景,你無需掌握Koa框架的語法也是可以輕松閱讀本文的。

初始化項目

創(chuàng)建項目目錄,并使用npm init初始化項目后,執(zhí)行下面操作:

安裝依賴
npm install mysql koa koa-router
創(chuàng)建index.js
// index.js

const Koa = require('koa');
const Router = require('koa-router');
const mysql = require('mysql');

const app = new Koa();
const router = new Router();

const connection = mysql.createConnection({
  host: 'localhost', // 填寫你的mysql host
  user: 'root', // 填寫你的mysql用戶名
  password: '123456' // 填寫你的mysql密碼
})

connection.connect(err => {
  if(err) throw err;
  console.log('mysql connncted success!');
})

router.get('/', ctx => {
  ctx.body = 'Visit index';
})
app.use(router.routes());

app.listen(3000);

在shell中執(zhí)行node index.js,當看到shell中打印出mysql connected success!,表明MySQL數(shù)據(jù)庫連接成功。

Node.js中操作MySQL數(shù)據(jù)庫的方法

打開瀏覽器, 訪問localhost:3000,當看到屏幕顯示Visit index時,表名項目初始化成功。

Node.js中操作MySQL數(shù)據(jù)庫的方法

數(shù)據(jù)庫操作

創(chuàng)建數(shù)據(jù)庫

當訪問/createdb時,創(chuàng)建一個mysqlkoa的數(shù)據(jù)庫,代碼如下:

router.get('/createdb', ctx => {
  return new Promise(resolve => {
    const sql = `CREATE DATABASE mysqlkoa`;

    connection.query(sql, (err) => {
      if (err) throw err;
      ctx.body = {
        code: 200,
        msg: `create database mysqlkoa success!`
      }
      resolve();
    });
  })
})

重新執(zhí)行node index.js,并使用瀏覽器訪問localhost:3000/createdb

Node.js中操作MySQL數(shù)據(jù)庫的方法

創(chuàng)建數(shù)據(jù)表

為了方便,我們直接在連接時使用剛才創(chuàng)建的數(shù)據(jù)庫,需要在mysql.createConnection中添加database:mysqlkoa的配置項。

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'mysqlkoa' // 添加該列
})

當訪問/createtable時,我們創(chuàng)建一個數(shù)據(jù)表fe_frame,該表用來保存前端框架的數(shù)據(jù):

router.get('/createtable', ctx => {
  return new Promise(resolve => {
    const sql = `CREATE TABLE fe_frame(
      id INT(11) AUTO_INCREMENT PRIMARY KEY,
      name VARCHAR(255),
      author VARCHAR(255)
    )`;
    connection.query(sql, (err ,results, filelds) => {
      if (err) throw err;
      ctx.body = {
        code: 200,
        msg: `create table of fe_frame success!`
      }
      resolve();
    })
  })
})

重新執(zhí)行node index.js,并使用瀏覽器訪問localhost:3000/createtable

Node.js中操作MySQL數(shù)據(jù)庫的方法

插入數(shù)據(jù)
插入單條數(shù)據(jù)

當訪問/insert時,用來插入單條數(shù)據(jù):

router.get('/insert', ctx => {
  return new Promise(resolve => {
    const sql = `INSERT INTO fe_frame(name, author)
    VALUES('vue', 'Evan')`;
    connection.query(sql, (err) => {
      if (err) throw err;
      ctx.body = {
        cde: 200,
        msg: `insert data to fe_frame success!`
      }
      resolve();
    })
  })
})

重新執(zhí)行node index.js,并使用瀏覽器訪問localhost:3000/insert

Node.js中操作MySQL數(shù)據(jù)庫的方法

插入多條數(shù)據(jù)

當訪問/insertmulti時,用來插入多條數(shù)據(jù):

router.get('/insertmulti', ctx => {
  return new Promise(resolve => {
    const sql = `INSERT INTO fe_frame(name, author)
    VALUES ?`;
    const values = [
      ['React', 'Facebook'],
      ['Angular', 'Google'],
      ['jQuery', 'John Resig']
    ];
    connection.query(sql, [values], (err, result) => {
      if (err) throw err;
      ctx.body = {
        code: 200,
        msg: `insert ${result.affectedRows} data to fe_frame success!`        
      }
      resolve();
    })
  })
})

重新執(zhí)行node index.js,并使用瀏覽器訪問localhost:3000/insertmulti

Node.js中操作MySQL數(shù)據(jù)庫的方法

使用phpMyAdmin訪問,可以看到此時mysqlkoa表如下

Node.js中操作MySQL數(shù)據(jù)庫的方法

刪除數(shù)據(jù)

當訪問/delete時,刪除相應(yīng)行。我們使用請求參數(shù)name來指定刪除哪個框架,在服務(wù)器端使用ctx.query.name獲取,代碼如下:

router.get('/delete', ctx => {
  return new Promise(resolve => {
    const name = ctx.query.name;
    const sql = `DELETE FROM fe_frame WHERE name = '${name}'`;
    connection.query(sql, (err, result) => {
      if (err) throw err;
      ctx.body = {
        code: 200,
        msg: `delete ${result.affectedRows} data from fe_frame success!`
      };
      resolve();
    })
  })
})

重新執(zhí)行node index.js,并使用瀏覽器訪問http://localhost:3000/delete?name=jQuery

Node.js中操作MySQL數(shù)據(jù)庫的方法

修改數(shù)據(jù)

當訪問/update時,更新vue框架的作者名為Evan You,代碼如下:

router.get('/update', ctx => {
  return new Promise(resolve => {
    const sql =  `UPDATE fe_frame SET author = 'Evan You' WHERE NAME = 'vue'`;
    connection.query(sql, (err, result) => {
      if (err) throw err;
      ctx.body = {
        code: 200,
        msg: `update ${result.affectedRows} data from fe_frame success!`
      };
      resolve();
    })
  })
})

重新執(zhí)行node index.js,并使用瀏覽器訪問http://localhost:3000/update

Node.js中操作MySQL數(shù)據(jù)庫的方法

查找數(shù)據(jù)

當訪問/select時,獲取滿足請求參數(shù)中框架名條件的項,代碼如下:

router.get('/select', ctx => {
  return new Promise(resolve => {
    let name = ctx.query.name;
    const sql = `SELECT * FROM fe_frame WHERE name = '${name}'`;
    connection.query(sql, (err, result) => {
      if (err) throw err;
      ctx.body = {
        code: 200,
        data: result
      }
      resolve();
    })
  })
})

重新執(zhí)行node index.js,并使用瀏覽器訪問http://localhost:3000/select?name=vue

Node.js中操作MySQL數(shù)據(jù)庫的方法

感謝各位的閱讀!關(guān)于“Node.js中操作MySQL數(shù)據(jù)庫的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

網(wǎng)頁題目:Node.js中操作MySQL數(shù)據(jù)庫的方法
分享路徑:http://chinadenli.net/article34/jiggse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司搜索引擎優(yōu)化網(wǎng)頁設(shè)計公司企業(yè)網(wǎng)站制作動態(tài)網(wǎng)站ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計