Practical learning: Let's talk about how Node.js operates the database

青灯夜游
Release: 2022-12-15 21:14:51
forward
2654 people have browsed it

This article shares the actual combat of Node.js server and introduces the method ofNodeto operate the database. I hope it will be helpful to everyone!

Practical learning: Let's talk about how Node.js operates the database

This series usesnodeas a record of the operation process of server development. It records the main content and organizes the context of the process to start with. The scholar's method records the learning content and gradually learns node from 0 to 1. The node framework based on express is used in the tutorial. [Related tutorial recommendations:nodejs video tutorial,Programming teaching

Connecting to the database

const mysql = require('mysql') const db = mysql.createPool({ host: 'localhost', user: 'root', password: '123123123', database: 'test', insecureAuth : true }) const sql = `select * from new_table` db.query(sql, (err, results) => { // console.log(err) if(err){ console.log(err.message) }else{ console.log(results) //查询语句返回的是数组 } })
Copy after login

First An error was reported immediately after connecting to the database for the first time. What else can I do? Just search it on Google

ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
Copy after login

Practical learning: Lets talk about how Node.js operates the database

It probably means that there are some operating permission issues involved and we need to go to the database. Execute this statement. If no error is reported, you can skip this step.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '这个地方替换成你的数据库密码';
Copy after login

Just execute it in mysqlworkbrench, and then go back to our code to continue the operation of connecting to the database

Practical learning: Lets talk about how Node.js operates the database

Proof when this statement is output The connection is already successful

Practical learning: Lets talk about how Node.js operates the database

insert statement

const obj = { name:'xiaoma', password:'666666' } const insertSql = `insert into new_table (name,password) values (?,?)` db.query(insertSql,[obj.name,obj.password],(err,res)=>{ if(err){ console.log(err.message) }else{ console.log(res) } })
Copy after login

Practical learning: Lets talk about how Node.js operates the database

affectedRows is the affected row. The number of affected rows is 1, indicating that the insert statement was executed successfully, so we can modify the judgment of successful insert here

if(res.affectedRows == 1){ console.log('insert success') }
Copy after login

Simplify the new sql

const obj = { name:'xiaoma', password:'123123' } const insertSql = `insert into new_table SET ?` db.query(insertSql,obj,(err,res)=>{ if(err){ console.log(err.message) } if(res.affectedRows == 1){ console.log('insert success') } })
Copy after login

update Statement

const updateSql = `Update new_table set name=? ,password=? where id=?` // const insertSql = `insert into new_table SET ?` db.query(updateSql,[obj.name,obj.password,obj.id],(err,res)=>{ if(err){ console.log(err.message) } if(res.affectedRows == 1){ console.log('insert success') } }) //简化写法 const updateSql = `Update new_table set ? where id=?` db.query(updateSql,[obj,obj.id],(err,res)=>{ })
Copy after login

delete statement

const updateSql = `delete from new_table where id=?` db.query(updateSql,5,(err,res)=>{ if(err){ console.log(err.message) } if(res.affectedRows == 1){ console.log('insert success') } })
Copy after login

For more node-related knowledge, please visit:nodejs tutorial!

The above is the detailed content of Practical learning: Let's talk about how Node.js operates the database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!