当前位置:网站首页>node. How to implement the SQL statement after JS connects to the database?
node. How to implement the SQL statement after JS connects to the database?
2022-07-01 09:24:00 【There is no choice but to succeed】
node.js Connect to database
These three things should be done well before using :
1、 Create an empty folder using Vscode open , Next, input
npm initInitialize it
2、 install mysql Plug in fornpm install mysql
3、 seepackage-lock.jsonWhether the file already exists mysql
Start connecting to database (MySQL)
1、 adopt require introduce mysql Plug in for ,
let mysql = require("mysql");
2、 Transfer mysql Under the createConnection() Method , This method has 4 Parameters need to be filled ;
host : The address of the connection ; such as :127.0.0.1 perhaps localhost
user : The user name of the connection ; such as :root
password: Connection password ; such as :root
database: Connected database ; such as :student
let connection = mysql.createConnection({
host: "127.0.0.1",
user: "root",
password: "root",
database: "student"
});
3、 adopt connent() Method to determine whether the data connection is successful !
connection.connect((err) => {
if (err) {
console.log(' The connection fails ' + err.stack);
return;
}
console.log(" Successful connection ");
})
4、 Executive execution sql sentence , Yes Two ways Optional , But they are all called query() Method to execute : Mode one :
Write in this method sql sentence ; Take the following query as an example ;
connection.query("select * from student", (err, result) => {
if (err) {
console.log(" The query fails " + err.message);
return;
}
console.log(' The query is successful ', result);
});
Mode two :
Declare a store outside sql The variable of ( If you need to submit parameters, such as executing the insert statement ); Parameters inserted values It's optional , So we can pass ? Way to occupy space !
let sql = "insert into student values(?,?,?)";
let params = ["3", " Wang Wu ", "14"];
connection.query(sql, params, (err, result) => {
if (err) {
console.log(" Insert the failure " + err.message);
return;
}
console.log(' Insert the success ');
})
5、 Close the connection
connection.end();
The overall code :
let mysql = require("mysql");
let connection = mysql.createConnection({
host: "127.0.0.1",
user: "root",
password: "root",
database: "student"
});
connection.connect((err) => {
if (err) {
console.log(' The connection fails ' + err.stack);
return;
}
console.log(" Successful connection ");
})
connection.query("select * from student", (err, result) => {
if (err) {
console.log(" The query fails " + err.message);
return;
}
console.log(' The query is successful ', result);
});
let sql = "insert into student values(?,?,?)";
let params = ["3", " Wang Wu ", "14"];
connection.query(sql, params, (err, result) => {
/
if (err) {
console.log(" Insert the failure " + err.message);
return;
}
console.log(' Insert the success ');
})
// Close the connection
connection.end();
边栏推荐
- 2022.02.15_ Daily question leetcode six hundred and ninety
- In the middle of the year, where should fixed asset management go?
- Differences between JS valueof and toString
- ES6 const essence and completely immutable implementation (object.free)
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
- Principle and application of single chip microcomputer timer, serial communication and interrupt system
- How to launch circle of friends marketing and wechat group activities
- Redis source code learning (29), compressed list learning, ziplist C (II)
- Exception handling of classes in C #
- 序列化、监听、自定义注解
猜你喜欢

Principles of Microcomputer - Introduction

How to realize the usage of connecting multiple databases in idel

nacos服务配置和持久化配置
![delete和delete[]引发的问题](/img/d9/a1c3e5ce51ef1be366a973aa42d1f0.png)
delete和delete[]引发的问题

Pain points and solutions of fixed assets management of group companies

How to manage fixed assets efficiently in one stop?

Network counting 01 physical layer

nacos简易实现负载均衡

Error org apache. catalina. core. StandardContext. FilterStart start filter exception

Bird recognition app
随机推荐
Short circuit operator lazy evaluation
Daily office consumables management solution
韦东山板子编译内核问题解决
FAQ | FAQ for building applications for large screen devices
手指点击屏幕就模拟进入F11进入全屏
ES6 decoupling top-level objects from windows
NoSQL数据库的安装和使用
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
Solution of EPS image blur by latex insertion
Latex插入的eps图片模糊解决方法
[pytorch] 2.4 convolution function nn conv2d
[pytorch learning] torch device
【pytorch】softmax函数
Tree structure -- binary tree 2 non recursive traversal
ES6-const本质与完全不可改实现(Object.freeze)
Meituan machine test in 2022
Installation and use of NoSQL database
Youqitong PE toolbox [vip] v3.7.2022.0106 official January 22 Edition
Is it safe to dig up money and make new shares
Nacos service configuration and persistence configuration