当前位置:网站首页>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();
边栏推荐
- 【pytorch】nn.AdaptiveMaxPool2d
- Understand shallow replication and deep replication through code examples
- Niuke monthly race 22- collect pieces of paper
- Weidongshan board compilation kernel problem solving
- nacos簡易實現負載均衡
- Vsync+ triple cache mechanism +choreographer
- 2.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data
- Wechat applet WebView prohibits page scrolling without affecting the implementation of overflow scrolling in the business
- Log4j log framework
- 【pytorch学习】torch.device
猜你喜欢
![[interview brush 101] linked list](/img/52/d159bc66c0dbc44c1282a96cf6b2fd.png)
[interview brush 101] linked list

3D printing Arduino four axis aircraft

FAQ | FAQ for building applications for large screen devices

樹結構---二叉樹2非遞歸遍曆

2.4 activation function

JS prototype chain

【pytorch】2.4 卷积函数 nn.conv2d

Principles of Microcomputer - Introduction

Pain points and solutions of equipment management in large factories
![2.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data](/img/6e/d8ef618127ac492f5142f7b600266d.png)
2.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data
随机推荐
小鸟识别APP
Differences between JS valueof and toString
es6-顶层对象与window的脱钩
NiO zero copy
闭包实现迭代器效果
PR training notes
Learning practice: comprehensive application of cycle and branch structure (II)
【pytorch】nn.AdaptiveMaxPool2d
2.4 activation function
nacos簡易實現負載均衡
韦东山板子编译内核问题解决
R language observation log (part24) -- initialization settings
2022.02.15_ Daily question leetcode six hundred and ninety
tensorrt yolov5_ trt. Py comments
dsPIC30F6014a LCD 方块显示
Daily office consumables management solution
2.3 [pytorch] data preprocessing torchvision datasets. ImageFolder
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
富文本实现插值
[ESP nanny level tutorial] crazy completion chapter - Case: ws2812 light control system based on Alibaba cloud, applet and Arduino