node && mysql 結合使用

來源:互聯網
上載者:User

標籤:資料   ++   上下   asc   下載   password   資料庫   class   cal   

本文基於mac上的開發,node && mysql的簡單結合使用.

一、搭建環境

  •   到官網上下載對應自己電腦的相關的版本。然後啟動mysql

  •   在使用npm -v 檢測本電腦是否安裝了nodejs,如果沒有就到node 官網下載相應的版本進行安裝,是本機上安裝的版本

  • 使用npm進行項目的搭建,需要安裝express和mysql,在建立好的目錄中,執行npm install express mysql --save
  • 這裡的express暫時沒有用上,可以不用安裝

二、項目的結構

  本項目是一個簡單的node 和 mysql的結合,所以結構相對來說比較簡單,如下

【題外話,本人使用的編輯器是vsc】

三、結合使用node&mysql

  • 執行相關的代碼進入資料庫,並建立資料庫,本人建立的測試的資料庫如下

  • 在建立的項目中輸入以下的代碼:
// 這裡是引入資料庫var mysql = require(‘mysql‘);//建立連結var connection = mysql.createConnection({    host:‘localhost‘,    user:‘root‘,    password:‘YES‘,    database:‘reng‘})//啟動連結connection.connect();//對資料庫進行操作connection.query(‘select * from students‘,function(err,rows,fields){    if(err){        console.log(err);    }    if(rows){        for(var i =0 ;i<rows.length;i++){            console.log(rows[i].name+‘-->‘+rows[i].age);        }    }})//取消連結connection.end();

四、觀察結果

  在相對應的檔案夾中執行 node app.js,會看到以下的結果

  

【結果和預期吻合】

【本文原創,引用請註明出處,謝謝】

 

node && mysql 結合使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.