写一个用web对数据库增删改查操作的语句
时间: 2023-11-30 19:02:52 浏览: 54
以下是使用 Web 技术(Node.js 和 Express)对 MySQL 数据库进行增删改查操作的示例代码:
首先,需要安装并导入相关的 npm 包:mysql 和 express。
```
const mysql = require('mysql');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// 创建连接池
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydb'
});
// 使用 body-parser 中间件解析请求体
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// 查询数据
app.get('/users', (req, res) => {
pool.getConnection((err, connection) => {
if (err) throw err;
connection.query('SELECT * FROM users', (err, results) => {
connection.release();
if (err) throw err;
res.send(results);
});
});
});
// 插入数据
app.post('/users', (req, res) => {
pool.getConnection((err, connection) => {
if (err) throw err;
const user = req.body;
connection.query('INSERT INTO users SET ?', user, (err, results) => {
connection.release();
if (err) throw err;
res.send(`${results.affectedRows} 条记录已插入`);
});
});
});
// 更新数据
app.put('/users/:id', (req, res) => {
pool.getConnection((err, connection) => {
if (err) throw err;
const user = req.body;
const { id } = req.params;
connection.query('UPDATE users SET ? WHERE id = ?', [user, id], (err, results) => {
connection.release();
if (err) throw err;
res.send(`${results.affectedRows} 条记录已更新`);
});
});
});
// 删除数据
app.delete('/users/:id', (req, res) => {
pool.getConnection((err, connection) => {
if (err) throw err;
const { id } = req.params;
connection.query('DELETE FROM users WHERE id = ?', id, (err, results) => {
connection.release();
if (err) throw err;
res.send(`${results.affectedRows} 条记录已删除`);
});
});
});
// 启动服务器
app.listen(3000, () => {
console.log('服务器已启动');
});
```
以上代码实现了查询、插入、更新和删除操作,使用了连接池来提高数据库操作性能。其中,使用了 Express 框架来处理 HTTP 请求和响应,使用了 body-parser 中间件来解析请求体。如果需要访问这些接口,可以使用 HTTP 工具(如 Postman)来发送请求。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)