Nodejs搭配Mysql

終于有一天,自己開始主動(dòng)寫demo和總結(jié)。
來看看怎么用強(qiáng)大的Nodejs來操作數(shù)據(jù)庫吧。

安裝mysql的過程就不說啦,直接看我的數(shù)據(jù)庫結(jié)構(gòu)吧

用戶信息表

1.連接數(shù)據(jù)庫

//connect.js
const mysql = require("mysql");

const connection = mysql.createConnection({
    host: "localhost",  //主機(jī)地址
    user: "root", //數(shù)據(jù)庫用戶名
    password: "123456", //數(shù)據(jù)庫用戶密碼
    database: "test"  //數(shù)據(jù)庫名
});

connection.connect();   //數(shù)據(jù)庫連接

connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results[0].solution);
});  //檢驗(yàn)連接成功

執(zhí)行 node connect.js
出現(xiàn)The solution is: 2表示連接成功

2.增加用戶
先連接數(shù)據(jù)庫再定義增加用戶的模板

//userInsert.js
const mysql = require("mysql");

const connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "123456",
    database: "test"
});

connection.connect();
//連接之后進(jìn)行插入操作

const addSql = 'insert user (id,name,sex,age,tel,mail,note) values(?,?,?,?,?,?,?)';
const addSqlParams = [null,"孫","man",13,"1234567","234567@qq.com","fghavi"];

connection.query(addSql,addSqlParams,function (err,result) {
    if(err){
        console.log('[INSERT ERROR] - ',err.message);
        return;
    }

    console.log('INSERT ID:',result);
});

connection.end();

執(zhí)行node userInsert.js

插入執(zhí)行結(jié)果g

3.查詢操作
查詢操作也要先鏈接數(shù)據(jù)庫再對(duì)數(shù)據(jù)庫進(jìn)行操作

const mysql = require("mysql");

const connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "123456",
    database: "test"  
});

connection.connect();
//連接之后進(jìn)行查詢操作

const sql = "select * from user"; //選擇所有
connection.query(sql,function (err, result) {
    if(err){
        console.log('[SELECT ERROR] - ',err.message);
        return;
    }
    console.log(result);
});  //輸出查詢結(jié)果

connection.end();
查詢操作結(jié)果

4.更新操作
根據(jù)數(shù)據(jù)庫的主鍵進(jìn)行查詢更新

//userUpdate.js
//連接數(shù)據(jù)庫與之前的相同  連接之后進(jìn)行修改
const modSql = "update user set name=?,sex=?,age=?,tel=?,mail=?,note=? where id = ?";
 //根據(jù)id進(jìn)行修改
const modSqlParams = ["及","woman",32,"345678","ndsajvn@qq.com","adsvfadsv",2];
//修改后的內(nèi)容

connection.query(modSql,modSqlParams,function (err, result) {
    if(err){
        console.log('[UPDATE ERROR] - ',err.message);
        return;
    }
    console.log('UPDATE success',result.affectedRows);
});
//輸出更新是否成功
connection.end();
更新操作

可以再對(duì)數(shù)據(jù)庫進(jìn)行查詢判斷是否更新成功

5.數(shù)據(jù)庫刪除
根據(jù)數(shù)據(jù)庫主鍵對(duì)某一條記錄進(jìn)行刪除操作

//userDelete.js
//l數(shù)據(jù)庫連接之后進(jìn)行刪除操作

const delSql = 'delete from user where id = "4"';
//根據(jù)id刪除
connection.query(delSql,function (err, result) {
    if(err){
        console.log('[DELETE ERROR] - ',err.message);
        return;
    }
    console.log('DELETE success',result.affectedRows);
});
connection.end();

執(zhí)行node userDelete.js 可以看到刪除結(jié)果,也可以通過數(shù)據(jù)庫查詢?cè)倥袛嗍欠駝h除成功。

6.遇到的問題

  • musql無法顯示中文
    alter table tbl_name convert to character set utf8;

就這么多啦~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容