当前位置:网站首页>Node connects to MySQL database and writes fuzzy query interface
Node connects to MySQL database and writes fuzzy query interface
2022-06-11 11:30:00 【Yanyan is very WOW!】
One 、 preparation
1. install mysql
2.node Install database in
// node Install in mysql
npm install mysql
3.node Connect to database
(1) stay db New file in directory sql.js
// sql.js
var mysql = require('mysql') // introduce
var connection = mysql.createConnection({
host: 'localhost', // Host name
user: 'root', // account number
password: 'root', // password
database: 'mysql', // The name of the library to be connected
})
module.exports = connection
(2) stay MySQL Create tables in the database 
Two 、 Write the interface
// stay routes Under folder index.js Write the following interfaces in
var express = require('express')
var router = express.Router()
var connection = require('../db/sql.js') // Bring in the connected database
router.get('/api/goods/search', function (req, res, next) {
// Interface
let [goodsName, orderName] = Object.keys(req.query) // Parameters obtained from the front end
console.log(goodsName, orderName)
let name = req.query.name
let order = req.query[orderName]
connection.query(
"select * from goods_search where name like '%" +
// Here is the fuzzy query , By name or discount perhaps oprice Fuzzy query
name +
"%' order by " +
orderName +
' ' +
order +
'',
function (error, result, fields) {
// if (error) throw error
res.send({
code: '0',
data: result,
})
}
)
})
module.exports = router
3、 ... and 、 The test results
// By name “ Big ” and discount Field fuzzy query , among asc In ascending order ,desc For the descending order 
// By name “ Small ” and discount Field fuzzy query 
边栏推荐
- Method of converting VOC format data set to Yolo format data set
- nft数字藏品系统品台搭建
- Want to be iron man? It is said that many big men use it to get started
- NFT digital collection system platform construction
- No category parents插件帮你去掉分类链接中的category前缀
- Vscade -- vscode multi window name is configured as project name
- Etcd介绍
- Enterprise wechat applet pit avoidance guide, welcome to add...
- How to form a good habit? By perseverance? By determination? None of them!
- Adapter mode -- can you talk well?
猜你喜欢

How programmers do sidelines

How to form a good habit? By perseverance? By determination? None of them!

设置默认收货地址【项目 商城】

适配器模式--能不能好好说话?

使用Yolov5训练好模型调用电脑自带摄像头时出现问题:TypeError: argument of type “int‘ is not iterable的解决方法

Interpretation of cube technology | past and present life of cube Rendering Design

Use yolov5 to train your own data set and get started quickly

Command mode - attack, secret weapon

Want to be iron man? It is said that many big men use it to get started

装饰模式--小美的生日蛋糕
随机推荐
National multi-year solar radiation spatial distribution data 1981-2022, temperature distribution data, evapotranspiration data, evaporation data, rainfall distribution data, sunshine data, wind speed
17.4 creating multiple threads, data sharing problem analysis and case code
设置默认收货地址【项目 商城】
Lifeifei: I am more like a scientist in physics than an engineer
WordPress用户名修改插件:Username Changer
NFT digital collection system platform construction
設置默認收貨地址【項目 商城】
Development of official account system for digital collection app applet
How to understand CPU load
Command mode - attack, secret weapon
WordPress landing page beautification plug-in: recommended by login Designer
Count the top k strings with the most occurrences
使用pydub修改wav文件的比特率,报错:C:\ProgramData\Anaconda3\lib\site-packages\pydub\utils.py:170: RuntimeWarning:
mongoDB 使用
Xiao P weekly Vol.08
数据库系统概论 ---- 第二章 -- 关系数据库(2.4 关系代数)
让WordPress支持注册用户上传自定义头像功能
Is the securities account opened by qiniu Gang safe and reliable?
木瓜移动CFO刘凡 释放数字时代女性创新力量
Create a folder in the WordPress Library