Node-RED中连接sqlserver数据库并实现增删改查的操作

Node-RED中连接sqlserver数据库并实现增删改查的操作

1、打开node-red 网页界面

2、安装NodeRED节点

节点名称:node-red-contrib-mssql

3 创建表

设计流程图

①inject 节点,只需要拖拽进页面即可,不需要做内容修改

② MSSQL 节点,输入信息,编辑数据库连接;输出创表语句

创表语句:

CREATE TABLE [user2] (id VARCHAR(128) NOT NULL,name VARCHAR(128) DEFAULT NULL,pass VARCHAR(128) DEFAULT NULL,create_time DATETIME DEFAULT GETDATE(),update_time DATETIME DEFAULT GETDATE(),PRIMARY KEY (id));

③ debug 节点,只需要拖拽进页面即可,不需要做内容修改

部署后,点击按钮执行创表操作

4 插入数据

设计流程图

inject 节点和debug节点操作,MSSQL 节点编辑连接操作和创表流程步骤相同,就不重复说明了

②MSSQL 节点,输入插入数据语句

 创表语句:

INSERT INTO [user2]  (id, name, pass)
VALUES ('2', 'luvk', '30');

5 更新语句

更新语句:

UPDATE [user2]
SET name = 'cc'
WHERE id = 1;

6 查看语句

查看语句:

select * from [user];

7 查看语句

删除语句:

DELETE FROM [user2]
WHERE id = 2;

总结:全流程json数据

[
    {
        "id": "0050d3b64520efc0",
        "type": "tab",
        "label": "sqlserver",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "50444b205365b7b1",
        "type": "inject",
        "z": "0050d3b64520efc0",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 430,
        "y": 160,
        "wires": [
            [
                "05980f64a43546e3"
            ]
        ]
    },
    {
        "id": "205e1dc78be2b277",
        "type": "debug",
        "z": "0050d3b64520efc0",
        "name": "debug 124",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 840,
        "y": 160,
        "wires": []
    },
    {
        "id": "0623f8dda03d7cd7",
        "type": "comment",
        "z": "0050d3b64520efc0",
        "name": "sqlserver 创建表",
        "info": "",
        "x": 160,
        "y": 160,
        "wires": []
    },
    {
        "id": "05980f64a43546e3",
        "type": "MSSQL",
        "z": "0050d3b64520efc0",
        "mssqlCN": "49a8339514344c57",
        "name": "",
        "query": "CREATE TABLE [user2] (id VARCHAR(128) NOT NULL,name VARCHAR(128) DEFAULT NULL,pass VARCHAR(128) DEFAULT NULL,create_time DATETIME DEFAULT GETDATE(),update_time DATETIME DEFAULT GETDATE(),PRIMARY KEY (id));",
        "outField": "payload",
        "x": 640,
        "y": 160,
        "wires": [
            [
                "205e1dc78be2b277"
            ]
        ]
    },
    {
        "id": "90aef4487e33db0f",
        "type": "comment",
        "z": "0050d3b64520efc0",
        "name": "sqlserver 插入值(固定值)",
        "info": "",
        "x": 200,
        "y": 460,
        "wires": []
    },
    {
        "id": "a63859c171ea7853",
        "type": "inject",
        "z": "0050d3b64520efc0",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 430,
        "y": 460,
        "wires": [
            [
                "f2388da6cc16b183"
            ]
        ]
    },
    {
        "id": "7bd4974d9b1b2336",
        "type": "debug",
        "z": "0050d3b64520efc0",
        "name": "debug 125",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 840,
        "y": 460,
        "wires": []
    },
    {
        "id": "f2388da6cc16b183",
        "type": "MSSQL",
        "z": "0050d3b64520efc0",
        "mssqlCN": "49a8339514344c57",
        "name": "",
        "query": "INSERT INTO [user2]  (id, name, pass)\nVALUES ('2', 'luvk', '30');",
        "outField": "payload",
        "x": 640,
        "y": 460,
        "wires": [
            [
                "7bd4974d9b1b2336"
            ]
        ]
    },
    {
        "id": "82c0cecdbe6cb93d",
        "type": "comment",
        "z": "0050d3b64520efc0",
        "name": "sqlserver 更新语句",
        "info": "",
        "x": 170,
        "y": 580,
        "wires": []
    },
    {
        "id": "7ea630879fe10a7c",
        "type": "inject",
        "z": "0050d3b64520efc0",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 430,
        "y": 580,
        "wires": [
            [
                "721c3549f471cd91"
            ]
        ]
    },
    {
        "id": "8549f456d149478e",
        "type": "debug",
        "z": "0050d3b64520efc0",
        "name": "debug 126",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 840,
        "y": 580,
        "wires": []
    },
    {
        "id": "721c3549f471cd91",
        "type": "MSSQL",
        "z": "0050d3b64520efc0",
        "mssqlCN": "49a8339514344c57",
        "name": "",
        "query": "UPDATE [user2]\nSET name = 'cc'\nWHERE id = 1;",
        "outField": "payload",
        "x": 640,
        "y": 580,
        "wires": [
            [
                "8549f456d149478e"
            ]
        ]
    },
    {
        "id": "247d024ec99b6548",
        "type": "inject",
        "z": "0050d3b64520efc0",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 430,
        "y": 260,
        "wires": [
            [
                "3d8078187ea32c21"
            ]
        ]
    },
    {
        "id": "d93cc43727388ca4",
        "type": "debug",
        "z": "0050d3b64520efc0",
        "name": "debug 127",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 840,
        "y": 260,
        "wires": []
    },
    {
        "id": "93265186c022908e",
        "type": "comment",
        "z": "0050d3b64520efc0",
        "name": "sqlserver 创建触发器create_time",
        "info": "",
        "x": 210,
        "y": 260,
        "wires": []
    },
    {
        "id": "3d8078187ea32c21",
        "type": "MSSQL",
        "z": "0050d3b64520efc0",
        "mssqlCN": "49a8339514344c57",
        "name": "",
        "query": "-- 创建触发器\nCREATE TRIGGER [user2_after_insert_trigger]\nON [user2]\nAFTER INSERT\nAS\nBEGIN\n    UPDATE [user2]\n    SET create_time = GETDATE()\n    WHERE id IN (SELECT id FROM inserted);\nEND;\n\n",
        "outField": "payload",
        "x": 640,
        "y": 260,
        "wires": [
            [
                "d93cc43727388ca4"
            ]
        ]
    },
    {
        "id": "afffde3e03143b47",
        "type": "inject",
        "z": "0050d3b64520efc0",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 430,
        "y": 360,
        "wires": [
            [
                "fd2b020e28f720fc"
            ]
        ]
    },
    {
        "id": "432d297b7b56b66b",
        "type": "debug",
        "z": "0050d3b64520efc0",
        "name": "debug 128",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 840,
        "y": 360,
        "wires": []
    },
    {
        "id": "da86016c2ecaeabe",
        "type": "comment",
        "z": "0050d3b64520efc0",
        "name": "sqlserver 创建触发器update_time",
        "info": "",
        "x": 210,
        "y": 360,
        "wires": []
    },
    {
        "id": "fd2b020e28f720fc",
        "type": "MSSQL",
        "z": "0050d3b64520efc0",
        "mssqlCN": "49a8339514344c57",
        "name": "",
        "query": "CREATE TRIGGER [user2_after_update_trigger]\nON [user2]\nAFTER UPDATE\nAS\nBEGIN\n    UPDATE [user2]\n    SET update_time = GETDATE()\n    WHERE id IN (SELECT id FROM inserted);\nEND;\n\n",
        "outField": "payload",
        "x": 640,
        "y": 360,
        "wires": [
            [
                "432d297b7b56b66b"
            ]
        ]
    },
    {
        "id": "b85037e9a7c7adeb",
        "type": "comment",
        "z": "0050d3b64520efc0",
        "name": "sqlserver 查看语句",
        "info": "",
        "x": 170,
        "y": 680,
        "wires": []
    },
    {
        "id": "e8137588ad614ddb",
        "type": "inject",
        "z": "0050d3b64520efc0",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 430,
        "y": 680,
        "wires": [
            [
                "5e12a9e7cbe29d1c"
            ]
        ]
    },
    {
        "id": "8f81bce657ac3e85",
        "type": "debug",
        "z": "0050d3b64520efc0",
        "name": "debug 129",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 830,
        "y": 680,
        "wires": []
    },
    {
        "id": "5e12a9e7cbe29d1c",
        "type": "MSSQL",
        "z": "0050d3b64520efc0",
        "mssqlCN": "49a8339514344c57",
        "name": "",
        "query": "select * from [user];",
        "outField": "payload",
        "x": 640,
        "y": 680,
        "wires": [
            [
                "8f81bce657ac3e85"
            ]
        ]
    },
    {
        "id": "c45827576367c305",
        "type": "comment",
        "z": "0050d3b64520efc0",
        "name": "sqlserver 删除语句",
        "info": "",
        "x": 170,
        "y": 780,
        "wires": []
    },
    {
        "id": "e50bd5706ed4e109",
        "type": "inject",
        "z": "0050d3b64520efc0",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 430,
        "y": 780,
        "wires": [
            [
                "db6f27bb68271e21"
            ]
        ]
    },
    {
        "id": "6ab8984efdf4fe38",
        "type": "debug",
        "z": "0050d3b64520efc0",
        "name": "debug 130",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 830,
        "y": 780,
        "wires": []
    },
    {
        "id": "db6f27bb68271e21",
        "type": "MSSQL",
        "z": "0050d3b64520efc0",
        "mssqlCN": "49a8339514344c57",
        "name": "",
        "query": "DELETE FROM [user2]\nWHERE id = 2;\n",
        "outField": "payload",
        "x": 640,
        "y": 780,
        "wires": [
            [
                "6ab8984efdf4fe38"
            ]
        ]
    },
    {
        "id": "49a8339514344c57",
        "type": "MSSQL-CN",
        "name": "",
        "server": "127.0.0.1",
        "encyption": true,
        "database": "mssql_jk"
    }
]

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
首先,安装必要的npm包: ``` npm install express mssql axios body-parser --save ``` 接下来,创建一个app.js文件: ```javascript const express = require('express'); const bodyParser = require('body-parser'); const mssql = require('mssql'); const axios = require('axios'); const app = express(); // 配置body-parser app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); // 配置mssql连接池 const pool = new mssql.ConnectionPool({ user: 'username', password: 'password', server: 'localhost', database: 'database', pool: { max: 10, min: 0, idleTimeoutMillis: 30000 } }); // 测试连接池是否正常工作 pool.connect().then(() => { console.log('Connected to MSSQL'); }).catch(err => { console.log(err); }); // 实现路由 app.get('/users', (req, res) => { pool.request() .query('SELECT * FROM users') .then(result => { res.json(result.recordset); }) .catch(err => { console.log(err); res.status(500).send('Server Error'); }); }); app.post('/users', (req, res) => { const { name, age, email } = req.body; pool.request() .input('name', name) .input('age', age) .input('email', email) .query('INSERT INTO users (name, age, email) VALUES (@name, @age, @email)') .then(() => { res.send('User added'); }) .catch(err => { console.log(err); res.status(500).send('Server Error'); }); }); app.put('/users/:id', (req, res) => { const { name, age, email } = req.body; const { id } = req.params; pool.request() .input('id', id) .input('name', name) .input('age', age) .input('email', email) .query('UPDATE users SET name = @name, age = @age, email = @email WHERE id = @id') .then(() => { res.send('User updated'); }) .catch(err => { console.log(err); res.status(500).send('Server Error'); }); }); app.delete('/users/:id', (req, res) => { const { id } = req.params; pool.request() .input('id', id) .query('DELETE FROM users WHERE id = @id') .then(() => { res.send('User deleted'); }) .catch(err => { console.log(err); res.status(500).send('Server Error'); }); }); // 启动服务器 app.listen(3000, () => { console.log('Server started on port 3000'); }); // 测试axios前后端数据通信 axios.get('http://localhost:3000/users') .then(res => console.log(res.data)) .catch(err => console.log(err)); axios.post('http://localhost:3000/users', { name: 'John', age: 30, email: 'john@example.com' }) .then(res => console.log(res.data)) .catch(err => console.log(err)); axios.put('http://localhost:3000/users/1', { name: 'John Doe', age: 32, email: 'johndoe@example.com' }) .then(res => console.log(res.data)) .catch(err => console.log(err)); axios.delete('http://localhost:3000/users/1') .then(res => console.log(res.data)) .catch(err => console.log(err)); ``` 注意:在使用mssql连接池之前,需要先安装mssql的驱动程序: ``` npm install mssql --save ``` 运行node app.js,即可启动服务器。使用axios发送请求,即可对数据库进行增删改查操作
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值