我如何在mysql中插入IP地址

问题描述 投票:0回答:1

早上好

我是节点red和javascript的新手,所以这可能是一个简单的问题,即时通讯试图插入一个IP地址到MySQL表在mysql中,我将该字段设置为varchar(20)

9/28/2019,9:08:38 AMnode:7bfc4d89.726dbc

消息:错误

“错误:ER_PARSE_ERROR:您的SQL语法有错误;请查看与MySQL服务器版本相对应的手册,以找到在第1行的'[object Object])'附近使用的正确语法”

基本流程如下:

[
  {
    "id": "df624ad9.0db0f8",
    "type": "function",
    "z": "661acf87.d0306",
    "name": "Defining inside ip",
    "func": "global.set(\"inside_ip\",msg.payload);\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "x": 530,
    "y": 120,
    "wires": [
      []
    ]
  },
  {
    "id": "400e4f07.42965",
    "type": "inject",
    "z": "661acf87.d0306",
    "name": "",
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "repeat": "1",
    "crontab": "",
    "once": true,
    "onceDelay": 0.1,
    "x": 190,
    "y": 120,
    "wires": [
      [
        "f3f24f92.29582"
      ]
    ]
  },
  {
    "id": "f3f24f92.29582",
    "type": "ip",
    "z": "661acf87.d0306",
    "name": "ip",
    "https": false,
    "timeout": "5000",
    "internalIPv4": true,
    "internalIPv6": false,
    "publicIPv4": false,
    "publicIPv6": false,
    "x": 330,
    "y": 120,
    "wires": [
      [
        "df624ad9.0db0f8"
      ]
    ]
  },
  {
    "id": "47202c3d.71ebfc",
    "type": "inject",
    "z": "661acf87.d0306",
    "name": "",
    "topic": "",
    "payload": "1",
    "payloadType": "num",
    "repeat": "1",
    "crontab": "",
    "once": true,
    "onceDelay": "5",
    "x": 230,
    "y": 180,
    "wires": [
      [
        "20c745e0.57fb2a"
      ]
    ]
  },
  {
    "id": "7bfc4d89.726dbc",
    "type": "mysql",
    "z": "661acf87.d0306",
    "mydb": "ca2479e3.1dda58",
    "name": "",
    "x": 510,
    "y": 180,
    "wires": [
      []
    ]
  },
  {
    "id": "20c745e0.57fb2a",
    "type": "function",
    "z": "661acf87.d0306",
    "name": "",
    "func": "msg.topic =\"INSERT INTO`node`(`inside_ip`) VALUES(\" +  global.get(\"inside_ip\") +  \")\";\nreturn msg;\n",
    "outputs": 1,
    "noerr": 0,
    "x": 350,
    "y": 180,
    "wires": [
      [
        "7bfc4d89.726dbc"
      ]
    ]
  },
  {
    "id": "ca2479e3.1dda58",
    "type": "MySQLdatabase",
    "z": "",
    "host": "72.90.182.40",
    "port": "3306",
    "db": "mysql_test",
    "tz": "eastern"
  }
]

但是如果我像这样设置“ ip”,它将起作用:

[

  {
    "id": "1f5e06b6.76e931",
    "type": "function",
    "z": "87908bbe.239128",
    "name": "Defining inside ip",
    "func": "global.set(\"inside_ip\",msg.payload);\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "x": 645,
    "y": 228,
    "wires": [
      []
    ]
  },
  {
    "id": "4dd624b5.e58f34",
    "type": "inject",
    "z": "87908bbe.239128",
    "name": "",
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "repeat": "1",
    "crontab": "",
    "once": true,
    "onceDelay": 0.1,
    "x": 305,
    "y": 228,
    "wires": [
      [
        "b9528c26.ceca2"
      ]
    ]
  },
  {
    "id": "b9528c26.ceca2",
    "type": "ip",
    "z": "87908bbe.239128",
    "name": "ip",
    "https": false,
    "timeout": "5000",
    "internalIPv4": true,
    "internalIPv6": false,
    "publicIPv4": false,
    "publicIPv6": false,
    "x": 445,
    "y": 228,
    "wires": [
      []
    ]
  },
  {
    "id": "215d3000.15a8b8",
    "type": "inject",
    "z": "87908bbe.239128",
    "name": "",
    "topic": "",
    "payload": "1",
    "payloadType": "num",
    "repeat": "1",
    "crontab": "",
    "once": true,
    "onceDelay": "5",
    "x": 345,
    "y": 288,
    "wires": [
      [
        "fdcd98f2.a35838"
      ]
    ]
  },
  {
    "id": "17946cb4.862c23",
    "type": "mysql",
    "z": "87908bbe.239128",
    "mydb": "59bd1ef5.938c2",
    "name": "",
    "x": 625,
    "y": 288,
    "wires": [
      []
    ]
  },
  {
    "id": "fdcd98f2.a35838",
    "type": "function",
    "z": "87908bbe.239128",
    "name": "",
    "func": "msg.topic =\"INSERT INTO`node`(`inside_ip`) VALUES(\" +  global.get(\"inside_ip\") +  \")\";\nreturn msg;\n",
    "outputs": 1,
    "noerr": 0,
    "x": 465,
    "y": 288,
    "wires": [
      [
        "17946cb4.862c23"
      ]
    ]
  },
  {
    "id": "a248bafe.b02ac",
    "type": "inject",
    "z": "87908bbe.239128",
    "name": "",
    "topic": "",
    "payload": "\"192.168.1.1\"",
    "payloadType": "str",
    "repeat": "",
    "crontab": "",
    "once": true,
    "onceDelay": 0.1,
    "x": 341,
    "y": 145,
    "wires": [
      [
        "1f5e06b6.76e931"
      ]
    ]
  },
  {
    "id": "59bd1ef5.938c2",
    "type": "MySQLdatabase",
    "z": "",
    "host": "72.90.182.40",
    "port": "3306",
    "db": "mysql_test",
    "tz": "eastern"
  }
]

任何建议都会很棒

问候迈克

mysql node-red
1个回答
0
投票

好,所以您使用的IP节点在msg.payload中返回以下内容:>

{“ internalIPv4”:“ 192.168.1.107”}

为了能够将其保存到数据库中,您需要裁剪:

({“ internalIPv4”:“和”}

有两种方法可以通过使用功能节点来做到这一点。

首先,一个是用任何内容替换不必要的字符串(这是我的方法!]

 var str = JSON.stringify(msg.payload)       
 var ip = str.replace('{"internalIPv4":"', ''); 
 ip = ip.replace('"}', '')
 msg.payload = ip
 return msg;

第二种方法是按字符数裁剪

   var str = JSON.stringify(msg.payload)
   var ip = str.substring(17,30 );
   msg.payload = ip
   return msg;

此代码的问题是,如果IP的长度发生更改,则IP中的一个数字可能会丢失。

这两个函数都返回相同的消息(第一个在函数之前,第二个在函数之后]

{“ internalIPv4”:“ 192.168.1.107”}

“ 192.168.1.107”

关于您的小型项目的四个提示:1.无需每秒发送SQL查询。

  1. 最好使用一些唯一的标识符(如果在树莓派上运行,请使用其序列号来标识哪个IP用于哪个设备)]] >>

  2. 在MYSQL上为IP地址添加UNIQUE键

  3. 在哪里不能使用“,请使用'(尤其是在MySQL查询中拟合数据)

  4. 希望我帮忙!

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.