Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install MySQL package for nodejs

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "nodejs how to install mysql package", the explanation content in the article is simple and clear, easy to learn and understand, please follow the idea of Xiaobian slowly in-depth, together to study and learn "nodejs how to install mysql package"!

This tutorial operating environment: Windows 7 system, nodejs version 14.15.4, DELL G3 computer.

nodejs install mysql package, connect MySQL method

First, install nodejs and npm. [Related recommendation: How to install nodejs in Windows environment?]

Write a simple small example, the whole small example uses the express framework, node_modules inside has downloaded the express and ejs template. Let's look at the table of contents first:

server.js =« Server startup files for node.js

db.js =》Database operation file

views =》Store template files, where all pages are stored

public => Place where all static resources are stored, such as css js images

How to connect MySQL:

The first step is to install an npm package called MySQL. This package is officially provided. Stability can be guaranteed. Of course, there are other npm packages. Here we only use mysql package. The installation method is very simple, enter the command npm install mysql --save, wait for the download to complete.

If you see this, it means the installation is successful.

Then we open the db.js file and write some code necessary to operate in the database, as shown in the following figure:

The code is very small, the meaning is also very obvious, the first step needs to introduce a mysql package, and then we create an empty object, and assign it a method called query, this method accepts two parameters, the first parameter is the sql statement when you query the data, the second parameter is the callback function to get the query result.

Looking inside the function, the first block of code is used to set the configuration of the operation mysql:

host indicates the address where mysql is installed, because I am a local database, so I use localhost directly.

user represents the user name of mysql

password indicates mysql password

database indicates the name of the specific library you want to select for operation

port indicates port, optional, default is 3306

The return value connection of mysql.createConnection is a concrete object of our next operation mysql, and all operation methods are based on it.

Call the connect method of connection to determine whether the connection is successful, and if it fails, print out the error message and stop running.

Call the query method of connection to send sql statement directly to the database, and return the result with callback function. There are three parameters in the callback function. The first parameter is the error object. If the operation fails, it will stop and print the error message. The second parameter is the specific returned result. Under normal circumstances, it is an array containing many jsons. The third parameter is also an array containing the explanation of each data, such as which library the current data belongs to. The watch. Wait. The second parameter we use the most is nature.

Close the connection when the database operation is finished

The whole process is very simple, but there are two problems, the first is the database connection loss problem, do not know if anyone encountered, the first visit to the home page, the connection database normal, the second visit to the home page, the database connection is not on, will report the error that the connection is lost. This reason is that we close the database connection every time we operate the database. When we visit it again, we can't find the connection, but the connection can't be closed. Some people may feel strange. Every time we visit the home page, we will visit the db.js file. Isn't there a new connection every time? Yes, but the code that generated the connection at the beginning was not placed in the db.query function, but placed outside, as shown below:

This results in a connection being generated only once, and after it is closed, the second interview does not get a connection. After placing it inside the function, use exports to expose the interface. Every time you visit the home page, you will go through the process of creating a connection again, and you will get a new connection every time, so there will be no problem accessing it. In fact, connection pools can be used directly in projects. Save a lot of trouble.

The second problem is that we introduced db.js in server.js

At this point you may see that mysql.query has two parameters, the first is sql, the second is a callback function, the callback function has a result parameter, in fact, he is the result of the database query. Some people will say why not use return directly in db.js to return the query results, what callback!

In fact, here are some problems caused by the asynchronous node.js. If we change the code in server.js to the following figure

Because we saw that the query method of the mysql package is asynchronous operation, this leads to the following res.render() method will not wait for him to query the results and then execute, often the results have not yet come out, they have already started rendering the page, but the data has not been obtained, so it will report an error. So we had to pass a callback function in, after mysql query method ends, the result is passed to our own callback function through parameters, so that we can get the result in the callback function. Then render it. Of course, to deal with this problem, you can also introduce a third-party package async to solve asynchronous problems, depending on the individual.

Thank you for reading, the above is the content of "nodejs how to install mysql package", after learning this article, I believe everyone has a deeper understanding of how to install mysql package in nodejs, and the specific use needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report