node和mysql的CURD总结

导语:之前做过一个小项目,其中用到了node和mysql,现在就结合这两者做一个使用操作总结。CURD是数据库技术中的缩写词,代表创建(Create)、更新(Update)、读取(Retrieve)和删除(Delete)操作,用于处理数据等操作。

目录

  • 前期准备
  • 实战演练
  • 方法总结

前期准备

安装php环境

  • 手动安装

建议阅读本博客另一篇文章《win10手动配置php开发环境教程》

  • 集成环境安装

可以使用这几个集成包:

PHPStudy
LNMP
XAMPP
wampserver
appserv

安装node环境

nodejs官网上面下载然后安装,一步一步的安装好了以下,查询下那个版本。

在这里插入图片描述

实战演练

本小节通过一个商品的增删改查来展示如何使用node来连接mysql进行各种操作。

node准备

  • 安装依赖包
$ mkdir demo
$ cd demo
$ npm init
$ npm install express --save
$ npm install -g express-generator
$ npm install pm2 -g
$ npm install supervisor
  • 启动node服务

编写启动脚本

// index.js
const express = require('express');
const app = express();
const port = 3000;
const db = require('./mysql');

app.get('/', (req, res) => {
   
    res.send('Hello,demo!');
})

app.listen(port, () => {
   
    console.log('Server is running on port ', port + '!');
})

package.json加入以下命令

"dev": "node index.js"

// package.json
{
   
  "name": "demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
   
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "node index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
   
    "express": "^4.17.1",
    "mysql": "^2.18.1",
    "supervisor": "^0.12.0"
  }
}

启动脚本

$ npm run start
# 或者
$ supervisor index.js

mysql准备

建立一个名叫demo的数据库,然后建一个名称为goods的数据表,接着进行如下结构的配置。

以下是具体的命令行操作方法。

  • 连接数据库
$ mysql -h 127.0.0.1 -P 3306 -u demo -p
# 输入密码后回车确认
Enter password: 

连接成功后如下所示:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 455
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
  • sql语句操作
-- 建立demo用户
mysql> CREATE USER 'demo'@'127.0.0.1' IDENTIFIED BY 'demo123456.';

-- 建立demo数据库
mysql> CREATE DATABASE demo;

-- 赋予用户demo操作数据库demo的权限
mysql> GRANT ALL PRIVILEGES ON demo.* TO 'demo'@'127.0.0.1';
FLUSH PRIVILEGES;

-- 创建数据表
mysql> CREATE TABLE `goods` (
  `id` int(11) NOT NULL COMMENT 'id',
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '名称',
  `number` int(11) NOT NULL COMMENT '数量',
  `price` int(11) NOT NULL COMMENT '价格',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间'
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='商品表';

node连接mysql

  • 安装依赖包

终于到了最关键的一步,首先安装mysql依赖包。回到demo目录下面,然后打开命令行。

mysql包地址

$ npm install mysql
  • 编写mysql文件
// mysql.js
const mysql = require('mysql');

const connection = mysql.createConnection({
   
    host: '127.0.0.1',
    user: 'demo',
    password: 'demo123456.',
    database: 'demo'
});

connection.connect(function (err) {
   
    if (err) {
   
        console.error
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值