Mysql 简明教程

MySQL - Node.js Syntax

Node.js 是一个 JavaScript 运行时环境,允许开发人员在网络浏览器外运行 JavaScript 代码,从而实现服务器端脚本化。

Node.js is a JavaScript runtime environment that allows developers to run JavaScript code outside of a web browser, enabling server-side scripting.

当我们讨论 Node.js MySQL 连接器时,我们指的是一个特定的库,它可以促进 Node.js 应用和 MySQL 数据库之间的通信。这款连接器允许开发人员与 MySQL 数据库进行交互,方法是提供简化使用 JavaScript 代码查询、更新和管理数据库内数据的的方法和功能。实质上,它充当一个桥梁,让 Node.js 应用程序无缝地连接到 MySQL 数据库中存储的数据并对其进行操作。

When we talk about a Node.js MySQL connector, we are referring to a specific library that facilitates communication between a Node.js application and a MySQL database. This connector enables developers to interact with a MySQL database by providing methods and functionalities that simplify tasks like querying, updating, and managing data within the database using JavaScript code. Essentially, it acts as a bridge, allowing Node.js applications to seamlessly connect with and manipulate data stored in a MySQL database.

Installation "mysql" package

要将 MySQL 与 Node.js 一起使用,你可以使用“mysql”包,这是 Node.js 中一个常用的 MySQL 驱动器。以下是安装 Node.js 和 MySQL 包的步骤:

To use MySQL with Node.js, you can use the "mysql" package, which is a popular MySQL driver for Node.js. Here are the steps to install Node.js and the MySQL package −

Step 1: Install Node.js

Step 1: Install Node.js

访问官方 Node.js 网站([role="bare"] [role="bare"]https://nodejs.org/ ),并为你的操作系统下载最新版本的 Node.js。遵循网站上提供的安装说明。

Visit the official Node.js website ([role="bare"]https://nodejs.org/) and download the latest version of Node.js for your operating system. Follow the installation instructions provided on the website.

Step 2: Create a Node.js Project

Step 2: Create a Node.js Project

创建你 Node.js 项目的新目录,并使用终端或命令提示符导航到它。

Create a new directory for your Node.js project and navigate to it using your terminal or command prompt.

mkdir mynodeproject
cd mynodeproject

Step 3: Initialize a Node.js Project

Step 3: Initialize a Node.js Project

运行以下命令来初始化一个新的 Node.js 项目。这将创建一个 'package.json' 文件。

Run the following command to initialize a new Node.js project. This will create a 'package.json' file.

npm init -y

Step 4: Install the MySQL Package

Step 4: Install the MySQL Package

使用以下命令安装“mysql”包:

Install the "mysql" package using the following command:

npm install mysql

Step 5: Create a JavaScript File

Step 5: Create a JavaScript File

在项目目录中创建一个 JavaScript 文件(例如 app.js)。

Create a JavaScript file (e.g., app.js) in your project directory.

Step 6: Run the Node.js Script

Step 6: Run the Node.js Script

使用以下命令运行你的 Node.js 脚本:

Run your Node.js script using the following command:

node app.js

恭喜你,你已经成功为你的 Node.js 项目安装了 MySQL 的 Node.js 连接器 (mysql 软件包)。

Now, you have successfully installed the MySQL Node.js connector (mysql package) for your Node.js project.

NodeJS Functions to Access MySQL

在 Node.js 中,"mysql" 软件包提供了一组用于与 MySQL 数据库交互的函数。以下是你能使用的一些重要函数:

In Node.js, the "mysql" package provides a set of functions to interact with MySQL databases. Here are some of the major functions you can use−

Basic Example

以下是使用 Node.js 连接和通信 MySQL 数据库的步骤:

Following are the steps to connect and communicate with a MySQL database using Node.js −

  1. Download and install Node.js

  2. Create a new directory, navigate to it, and run 'npm init -y'.

  3. Run 'npm install mysql'.

  4. Create a JavaScript file (e.g., app.js) and use the "mysql" package to connect to the MySQL database.

  5. Use the query or execute functions to perform SQL queries on the database.

  6. Implement error handling for database operations. Close the database connection when finished.

  7. Execute your Node.js script with node app.js.

以下示例展示了 NodeJS 的一个通用语法以调用任意 MySQL 查询。

The following example shows a generic syntax of NodeJS to call any MySQL query.

const mysql = require("mysql2");

// Create a connection to the MySQL database
const connection = mysql.createConnection({
  host: 'your-mysql-hostname',
  user: 'your-mysql-username',
  password: 'your-mysql-password',
  database: 'your-mysql-database',
});

// Connect to the database
connection.connect((err) => {
  if (err) {
    console.error('Error connecting to MySQL:', err);
    return;
  }
  console.log('Connected to MySQL database');

  // Perform MySQL operations here
  connection.query("SELECT * FROM your_table", (err, results) => {
    if (err) throw err;
    console.log('Query result:', results);
  });

  // Close the connection when done
  connection.end((err) => {
    if (err) console.error('Error closing MySQL connection:', err);
    else console.log('Connection closed');
  });
});