如何将ip地址插入mysql

如何将ip地址插入mysql,mysql,node-red,Mysql,Node Red,早上好 我不熟悉node red和javascript,所以这可能是我遇到的一个简单问题 我试图在mysql表中插入一个ip地址 在mysql中,我将字段设置为varchar(20) 2019年9月28日,9:08:38 AMnode:7bfc4d89.726dbc msg:错误 “错误:ER_PARSE_错误:SQL语法有错误;请查看与MySQL服务器版本对应的手册,以了解第1行“[object])”附近使用的正确语法” 基本流程如下: [ { "id": "df624ad9.0d

早上好

我不熟悉node red和javascript,所以这可能是我遇到的一个简单问题 我试图在mysql表中插入一个ip地址 在mysql中,我将字段设置为varchar(20)

2019年9月28日,9:08:38 AMnode:7bfc4d89.726dbc

msg:错误

“错误:ER_PARSE_错误:SQL语法有错误;请查看与MySQL服务器版本对应的手册,以了解第1行“[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"
  }
]
任何建议都很好


mike

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

{“internalIPv4”:“192.168.1.107”}

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

({“internalIPv4”:“和”}

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

首先,一个是用零替换不必要的字符串(这是我的方法!)

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

   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查询

  • 最好使用一些唯一的标识符(如果在raspberry pi上运行,则使用其序列号来标识哪个IP用于哪个设备)

  • 在MYSQL上为IP地址添加唯一密钥

  • 在不能使用“use”的情况下(尤其是在MySQL查询中拟合数据时)


  • 希望我能帮上忙!

    问题是AMnode应该是AM,node,这很可能是导致问题的原因,但您的示例中没有包含B var str = JSON.stringify(msg.payload) var ip = str.substring(17,30 ); msg.payload = ip return msg;